7 Ekim 2017

C# Bazı Matris İşlemleri

using System;

namespace EigenvalueEigenvector
{
    class Program
    {
        static void Main(string[] args)
        {
            var n = ReadValue<int>("Matrix dimension");
            var matrix = CreateMatrix(n);
            Console.WriteLine("Transpoze");
            MatrixPrint(Transpoze(matrix));
            Console.WriteLine("Determinant");
            Console.WriteLine(Determinant(matrix));
            Console.WriteLine("Inverse");
            MatrixPrint(Inverse(matrix));
            Console.WriteLine("Sum");
            MatrixPrint(MatrixSum(matrix, matrix));
            Console.WriteLine("Multiplication");
            MatrixPrint(MatrixMultiplication(matrix, Inverse(matrix)));


        }

        static T ReadValue<T>(string message)
        {
            while (true)
            {
                Console.WriteLine(message);
                string line = Console.ReadLine();
                try
                {
                    return (T)(Convert.ChangeType(line, typeof(T)));

                }
                catch (Exception)
                {
                    Console.WriteLine("Hatalı format! Geçerli bir format girin");
                }
            }
        }
        static Double[,] CreateMatrix(int m, int n)
        {
            var matrix = new double[m, n];
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    matrix[i, j] = ReadValue<double>($"a[{i + 1},{j + 1}] değerini giriniz");
                }
            }
            MatrixPrint(matrix);
            return matrix;
        }
        static Double[,] CreateMatrix(int n)
        {
            return CreateMatrix(n, n);
        }

        static double Determinant(double[,] m)
        {

            if (m.GetLength(0) != m.GetLength(1))
                throw new Exception("Kare matrix değil!");
            int n = m.GetLength(0);
            if (n == 2)
                return m[0, 0] * m[1, 1] - m[0, 1] * m[1, 0];

            if (n == 1)
                return Convert.ToDouble(m[0, 0]);


            var sum = .0;

            for (int i = 0; i < n; i++)
            {
                double q = 1;
                var x = -1 + i;
                for (int j = 0; j < n; j++)
                {
                    x++;
                    q *= Convert.ToDouble(m[x % n, j]);
                }
                sum += q;
            }
            for (int i = 0; i < n; i++)
            {
                double q = 1;
                var x = i + 1;
                for (int j = 0; j < n; j++)
                {
                    x--;
                    q *= Convert.ToDouble(m[Mod(x, n), j]);
                }
                sum -= q;
            }
            return sum;
        }

        static int Mod(int value, int mod)
        {
            int a = value % mod;
            return a < 0 ? mod + a : a;
        }

        static double[,] Transpoze(double[,] m)
        {
            double[,] result = new double[m.GetLength(1), m.GetLength(0)];
            for (int i = 0; i < m.GetLength(1); i++)
            {
                for (int j = 0; j < m.GetLength(0); j++)
                {
                    result[i, j] = m[j, i];
                }
            }
            return result;
        }
        static void MatrixPrint(double[,] m)
        {
            for (int i = 0; i < m.GetLength(0); i++)
            {
                for (int j = 0; j < m.GetLength(1); j++)
                {
                    Console.Write($"{m[i, j]}\t");
                }
                Console.WriteLine();
            }
            Console.WriteLine("----------------------------------------------");
        }

        static double[,] MinorMatrix(double[,] matrix, int x, int y)
        {
            var m = matrix.GetLength(0);
            var n = matrix.GetLength(1);
            if (m < x || n < y)
                throw new Exception("Row or Colum does not exist in the matrix");
            double[,] result = new double[m - 1, n - 1];
            int _i = 0;

            for (int i = 0; i < m; i++)
            {
                int _j = 0;
                if (i == x)
                    continue;
                for (int j = 0; j < n; j++)
                {
                    if (j == y)
                        continue;
                    result[_i, _j] = matrix[i, j];

                    _j++;
                }
                _i++;
            }

            return result;
        }
        static double Minor(double[,] matrix, int x, int y)
        {
            return Determinant(MinorMatrix(matrix, x, y));
        }
        static double Cofactor(double[,] matrix, int x, int y)
        {
            var minor = Minor(matrix, x, y);
            return minor * ((x + y) % 2 == .0 ? 1 : -1);
        }

        static double[,] Inverse(double[,] matrix)
        {
            var det = Determinant(matrix);
            if (det == 0)
                throw new Exception("This matrix's determinant is 0");
            var n = matrix.GetLength(0);

            var result = new double[n, n];
            if (n == 1)
            {
                result[0, 0] = n / det;
                return result;
            }
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    result[i, j] = Cofactor(matrix, i, j) / det;
                }
            }

            result = Transpoze(result);
            MatrixPrint(result);
            return result;
        }
        static double[,] MatrixSum(double[,] x, double[,] y)
        {
            if ((x.GetLength(0) != y.GetLength(0)) || (x.GetLength(1) != y.GetLength(1)))
                throw new Exception("these matrixes dimens not equal each other");
            double[,] result = new double[x.GetLength(0), x.GetLength(1)];
            for (int i = 0; i < x.GetLength(0); i++)
            {
                for (int j = 0; j < x.GetLength(1); j++)
                {
                    result[i, j] = x[i, j] + y[i, j];
                }
            }
            return result;
        }

        static double[,] MatrixMultiplication(double[,] x, double[,] y)
        {
            var x_m = x.GetLength(0);
            var x_n = x.GetLength(0);
            var y_m = y.GetLength(1);
            var y_n = y.GetLength(1);

            if (x_n != y_m)
                throw new Exception("dimension error");

            var result = new double[x_m, y_n];
            for (int i = 0; i < x_m; i++)
            {

                for (int z = 0; z < x_n; z++)
                {
                    var _x = .0;
                    for (int j = 0; j < x_n; j++)
                    {
                        _x += x[i, j] * y[j, z];
                    }
                    result[i, z] = _x;

                }
            }
            return result;



        }

    }
}