invers matrix-manji




TUGAS  UAS
ALJABAR LINEAR DAN MATRIKS





           
            Nama                             : WILHELMUS MANJI JOMAN
Nim                      : 1606080044





Jurusan Ilmu Komputer
Fakultas Sains Dan Teknik
Universitas Nusa Cendana







SYNTAX  INVERS  MATRIKS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication82
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.SetCursorPosition(40, 0); Console.WriteLine("-----------------------------------");
            Console.SetCursorPosition(40, 1); Console.WriteLine("|Program By: Wilhelmus Manji Joman|");
            Console.SetCursorPosition(40, 2); Console.WriteLine("***********************************");
            Console.SetCursorPosition(20,4); Console.WriteLine("APLIKASI PENGHITUNGAN INVERS MATRIKS");
            Console.SetCursorPosition(19, 5); Console.WriteLine("*________________**_________________*");
            Console.SetCursorPosition(1, 7); Console.Write("Masukan Ordo Matrix(baris,kolom) : ");
            int pilihan = int.Parse(Console.ReadLine());

            if (pilihan == 2)
            {
                Console.Clear();

                int[,] matriks = new int[2, 2];
                int a, b;

                Console.SetCursorPosition(20, 1); Console.WriteLine("Invers Dengan Adjoin Matriks Ordo 2x2");
                Console.SetCursorPosition(20, 2); Console.WriteLine("-----------------------------------------");
                Console.ReadLine();

                for (a = 0; a < 2; a++)
                {
                    for (b = 0; b < 2; b++)
                    {
                        Console.Write("Matriks{0},{1} = ", a, b);
                        matriks[a, b] = int.Parse(Console.ReadLine());
                    }
                }

                for (a = 0; a < 2; a++)
                {
                    for (b = 0; b < 2; b++)
                    {
                        Console.Write("\t" + matriks[a, b]);

                    }
                    Console.WriteLine();
                }
                Console.ReadLine();

                int[,] adj_matrix = new int[2, 2];

                adj_matrix[0, 0] = matriks[1, 1];
                adj_matrix[0, 1] = (matriks[0, 1] * -1);
                adj_matrix[1, 0] = matriks[1, 0] * -1;
                adj_matrix[1, 1] = matriks[0, 0];

                Console.Write("Adjoin Matriks = ");
                Console.Write("\n" + adj_matrix[0, 0]);
                Console.Write("\t" + adj_matrix[0, 1]);
                Console.Write("\n" + adj_matrix[1, 0]);
                Console.Write("\t" + adj_matrix[1, 1]);

                Console.ReadLine();

                double x;
                double Y;

                x = ((matriks[0, 0] * matriks[1, 1]) - (matriks[0, 1] * matriks[1, 0]));
                Y = 1 / x;

                Console.WriteLine("Determinan = " + x);

                double[,] mx = new double[2, 2];

                Console.WriteLine("Invers Matriks =");
                for (int i = 0; i < 2; i++)
                {
                    for (int j = 0; j < 2; j++)
                    {
                        Console.Write(mx[i, j] = adj_matrix[i, j] * Y);
                        Console.Write("\t");
                    }
                    Console.WriteLine();
                }

                Console.ReadLine();
            }


            if (pilihan == 3)
            {
                Console.Clear();

                int[,] ordo = new int[3, 3];
                int f, g;

                Console.SetCursorPosition(20, 1); Console.WriteLine("Invers Dengan Adjoin Matriks Ordo 3x3");
                Console.SetCursorPosition(20, 2); Console.WriteLine("------------------------------------------");
                Console.ReadLine();

                for (f = 0; f < 3; f++)
                {
                    for (g = 0; g < 3; g++)
                    {
                        Console.Write("Matriks {0},{1} = ", f, g);
                        ordo[f, g] = int.Parse(Console.ReadLine());
                    }
                }
                Console.ReadLine();

                Console.WriteLine("Menampilkan Matriks = ");
                for (f = 0; f < 3; f++)
                {
                    for (g = 0; g < 3; g++)
                    {
                        Console.Write("\t" + ordo[f, g]);
                    }
                    Console.WriteLine();
                }

                Console.ReadLine();

                int A, a1, a2, a3, B, b1, b2, b3, C;
                a1 = (ordo[0, 0] * ordo[1, 1] * ordo[2, 2]);
                a2 = (ordo[0, 1] * ordo[1, 2] * ordo[2, 0]);
                a3 = (ordo[0, 2] * ordo[1, 0] * ordo[2, 1]);
                A = (a1 + a2 + a3);

                b1 = (ordo[0, 1] * ordo[1, 0] * ordo[2, 2]);
                b2 = (ordo[0, 0] * ordo[1, 2] * ordo[2, 1]);
                b3 = (ordo[0, 2] * ordo[1, 1] * ordo[2, 0]);
                B = (b1 + b2 + b3);

                C = (a1 + a2 + a3) - (b1 + b2 + b3);

                Console.Write("Determinan = ");
                Console.Write("" + C);
                Console.WriteLine();

                int[,] ad = new int[3, 3];
                int n, m;

                ad[0, 0] = (ordo[1, 1] * ordo[2, 2]) - (ordo[1, 2] * ordo[2, 1]);
                ad[0, 1] = (-1 * ((ordo[1, 0] * ordo[2, 2]) - (ordo[1, 2] * ordo[2, 0])));
                ad[0, 2] = (ordo[1, 0] * ordo[2, 1]) - (ordo[1, 1] * ordo[2, 0]);
                ad[1, 0] = (-1 * ((ordo[0, 1] * ordo[2, 2]) - (ordo[0, 2] * ordo[2, 1])));
                ad[1, 1] = (ordo[0, 0] * ordo[2, 2]) - (ordo[0, 2] * ordo[2, 0]);
                ad[1, 2] = (-1 * ((ordo[0, 0] * ordo[2, 1]) - (ordo[0, 1] * ordo[2, 0])));
                ad[2, 0] = (ordo[0, 1] * ordo[2, 2]) - (ordo[0, 2] * ordo[2, 1]);
                ad[2, 1] = (-1 * ((ordo[0, 0] * ordo[1, 2]) - (ordo[0, 2] * ordo[1, 0])));
                ad[2, 2] = (ordo[0, 0] * ordo[1, 1]) - (ordo[0, 1] * ordo[1, 0]);

                Console.ReadLine();

                Console.WriteLine("Adjoin Matriks = ");
                for (n = 0; n < 3; n++)
                {
                    for (m = 0; m < 3; m++)
                    {
                        Console.Write("\t" + ad[n, m]);
                    }
                    Console.WriteLine();

                }
                Console.ReadLine();

                int[,] baru = new int[3, 3];
                int y, z;

                for (y = 0; y < 3; y++)
                {
                    for (z = 0; z < 3; z++)
                    {
                        baru[z, y] = ad[y, z];
                    }
                }


                for (y = 0; y < 3; y++)
                {
                    for (z = 0; z < 3; z++)
                    {
                        Console.Write("\t" + baru[y, z]);
                    }
                    Console.WriteLine();
                }
                Console.ReadLine();


                double[,] has = new double[3, 3];


                if (C == 0)
                {

                    for (y = 0; y < 3; y++)
                    {
                        for (z = 0; z < 3; z++)
                        {
                            has[y, z] = 0;
                            Console.Write("\t" + has[y, z]);
                        }
                        Console.WriteLine();
                    }
                    Console.ReadLine();
                }

                else
                {
                    Console.WriteLine("Invers Matrix = ");
                    for (y = 0; y < 3; y++)
                    {
                        for (z = 0; z < 3; z++)
                        {
                            has[y, z] = (baru[y, z]);
                            has[y, z] = has[y, z] / C;
                            Console.Write("\t" + has[y, z]);
                        }
                        Console.WriteLine();
                    }
                    Console.ReadLine();
                }

            }

            if (pilihan == 4)
            {
                Console.Clear();

                Console.SetCursorPosition(20, 1); Console.WriteLine("Invers Dengan Adjoin Matriks 4x4");
                Console.SetCursorPosition(20, 2); Console.WriteLine("-------------------------------------");
                Console.ReadLine();

                int[,] m1 = new int[4, 4];
                int a, b;

                for (a = 0; a < 4; a++)
                {
                    for (b = 0; b < 4; b++)
                    {
                        Console.Write("Matriks[{0},{1}] = ", a, b);
                        m1[a, b] = int.Parse(Console.ReadLine());
                    }
                }

                for (a = 0; a < 4; a++)
                {
                    for (b = 0; b < 4; b++)
                    {
                        Console.Write("\t" + m1[a, b]);
                    }
                    Console.WriteLine();
                }
                Console.ReadLine();

                double det;

                det = (m1[0, 0] * m1[1, 1] * m1[2, 2] * m1[3, 3]) + (m1[0, 0] * m1[1, 2] * m1[2, 3] * m1[3, 1]) + (m1[0, 0] * m1[1, 3] * m1[2, 1] * m1[3, 2])
                      + (m1[0, 1] * m1[1, 0] * m1[2, 3] * m1[3, 2]) + (m1[0, 1] * m1[1, 2] * m1[2, 0] * m1[3, 3]) + (m1[0, 1] * m1[1, 3] * m1[2, 2] * m1[3, 0])
                      + (m1[0, 2] * m1[1, 0] * m1[2, 1] * m1[3, 3]) + (m1[0, 2] * m1[1, 1] * m1[2, 3] * m1[3, 0]) + (m1[0, 2] * m1[1, 3] * m1[2, 0] * m1[3, 1])
                      + (m1[0, 3] * m1[1, 0] * m1[2, 2] * m1[3, 1]) + (m1[0, 3] * m1[1, 1] * m1[2, 0] * m1[3, 2]) + (m1[0, 3] * m1[1, 2] * m1[2, 1] * m1[3, 0])
                      - (m1[0, 0] * m1[1, 1] * m1[2, 3] * m1[3, 2]) - (m1[0, 0] * m1[1, 2] * m1[2, 1] * m1[3, 3]) - (m1[0, 0] * m1[1, 3] * m1[2, 2] * m1[3, 1])
                      - (m1[0, 1] * m1[1, 0] * m1[2, 2] * m1[3, 3]) - (m1[0, 1] * m1[1, 2] * m1[2, 3] * m1[3, 0]) - (m1[0, 1] * m1[1, 3] * m1[2, 0] * m1[3, 2])
                      - (m1[0, 2] * m1[1, 0] * m1[2, 3] * m1[3, 1]) - (m1[0, 2] * m1[1, 1] * m1[2, 0] * m1[3, 3]) - (m1[0, 2] * m1[1, 3] * m1[2, 1] * m1[3, 0])
                      - (m1[0, 3] * m1[1, 0] * m1[2, 1] * m1[3, 2]) - (m1[0, 3] * m1[1, 1] * m1[2, 2] * m1[3, 0]) - (m1[0, 3] * m1[1, 2] * m1[2, 0] * m1[3, 1]);

                {
                    double[,] kofak = new double[4, 4];
                    double c, d;

                    for (c = 0; c < 4; c++)
                    {
                        for (d = 0; d < 4; d++)
                        {
                            kofak[0, 0] = (m1[1, 1] * m1[2, 2] * m1[3, 3]) + (m1[1, 2] * m1[2, 3] * m1[3, 1]) + (m1[1, 3] * m1[2, 1] * m1[3, 2])
                                            - (m1[1, 1] * m1[2, 3] * m1[3, 2]) - (m1[1, 2] * m1[2, 1] * m1[3, 3]) - (m1[1, 3] * m1[2, 2] * m1[3, 1]);
                            kofak[0, 1] = (m1[0, 1] * m1[2, 3] * m1[3, 2]) + (m1[0, 2] * m1[2, 1] * m1[3, 3]) + (m1[0, 3] * m1[2, 2] * m1[3, 1])
                                            - (m1[0, 1] * m1[2, 2] * m1[3, 3]) - (m1[0, 2] * m1[2, 3] * m1[3, 1]) - (m1[0, 3] * m1[2, 1] * m1[3, 2]);
                            kofak[0, 2] = (m1[0, 1] * m1[1, 2] * m1[3, 3]) + (m1[0, 2] * m1[1, 3] * m1[3, 1]) + (m1[0, 3] * m1[1, 1] * m1[3, 2])
                                            - (m1[0, 1] * m1[1, 3] * m1[3, 2]) - (m1[0, 2] * m1[1, 1] * m1[3, 3]) - (m1[0, 3] * m1[1, 2] * m1[3, 1]);
                            kofak[0, 3] = (m1[0, 1] * m1[1, 3] * m1[2, 2]) + (m1[0, 2] * m1[1, 1] * m1[2, 3]) + (m1[0, 3] * m1[1, 2] * m1[2, 1])
                                            - (m1[0, 1] * m1[1, 2] * m1[2, 3]) - (m1[0, 2] * m1[1, 3] * m1[2, 1]) - (m1[0, 3] * m1[1, 1] * m1[2, 2]);

                            kofak[1, 0] = (m1[1, 0] * m1[2, 3] * m1[3, 2]) + (m1[1, 2] * m1[2, 0] * m1[3, 3]) + (m1[1, 3] * m1[2, 2] * m1[3, 0])
                                            - (m1[1, 0] * m1[2, 2] * m1[3, 3]) - (m1[1, 2] * m1[2, 3] * m1[3, 0]) - (m1[1, 3] * m1[2, 0] * m1[3, 2]);
                            kofak[1, 1] = (m1[0, 0] * m1[2, 2] * m1[3, 3]) + (m1[0, 2] * m1[2, 3] * m1[3, 0]) + (m1[0, 3] * m1[2, 0] * m1[3, 2])
                                            - (m1[0, 0] * m1[2, 3] * m1[3, 2]) - (m1[0, 2] * m1[2, 0] * m1[3, 3]) - (m1[0, 3] * m1[2, 2] * m1[3, 0]);
                            kofak[1, 2] = (m1[0, 0] * m1[1, 3] * m1[3, 2]) + (m1[0, 2] * m1[1, 0] * m1[3, 3]) + (m1[0, 3] * m1[1, 2] * m1[3, 0])
                                            - (m1[0, 0] * m1[1, 2] * m1[3, 3]) - (m1[0, 2] * m1[1, 3] * m1[3, 0]) - (m1[0, 3] * m1[1, 0] * m1[3, 2]);
                            kofak[1, 3] = (m1[0, 0] * m1[1, 2] * m1[1, 3]) + (m1[0, 2] * m1[1, 3] * m1[2, 0]) + (m1[0, 3] * m1[1, 0] * m1[2, 2])
                                            - (m1[0, 0] * m1[1, 3] * m1[2, 2]) - (m1[0, 2] * m1[1, 0] * m1[2, 3]) - (m1[0, 3] * m1[1, 2] * m1[2, 0]);

                            kofak[2, 0] = (m1[1, 0] * m1[2, 1] * m1[2, 2]) + (m1[1, 1] * m1[2, 3] * m1[3, 0]) + (m1[1, 3] * m1[2, 0] * m1[3, 1])
                                            - (m1[1, 0] * m1[2, 3] * m1[3, 1]) - (m1[1, 1] * m1[2, 0] * m1[2, 2]) - (m1[1, 3] * m1[2, 1] * m1[3, 0]);
                            kofak[2, 1] = (m1[0, 0] * m1[2, 3] * m1[3, 1]) + (m1[0, 1] * m1[2, 0] * m1[2, 2]) + (m1[0, 3] * m1[2, 1] * m1[3, 0])
                                            - (m1[0, 0] * m1[2, 1] * m1[2, 2]) - (m1[0, 1] * m1[2, 3] * m1[3, 0]) - (m1[0, 3] * m1[2, 0] * m1[3, 1]);
                            kofak[2, 2] = (m1[0, 0] * m1[1, 1] * m1[3, 3]) + (m1[0, 1] * m1[1, 3] * m1[3, 0]) + (m1[0, 3] * m1[1, 0] * m1[3, 1])
                                            - (m1[0, 0] * m1[1, 3] * m1[3, 1]) - (m1[0, 1] * m1[1, 0] * m1[3, 3]) - (m1[0, 3] * m1[1, 1] * m1[3, 0]);
                            kofak[2, 3] = (m1[0, 0] * m1[1, 3] * m1[2, 1]) + (m1[0, 1] * m1[1, 0] * m1[2, 3]) + (m1[0, 3] * m1[1, 1] * m1[2, 0])
                                            - (m1[0, 0] * m1[1, 1] * m1[2, 3]) - (m1[0, 1] * m1[1, 3] * m1[2, 0]) - (m1[0, 3] * m1[1, 0] * m1[2, 1]);

                            kofak[3, 0] = (m1[1, 0] * m1[2, 2] * m1[3, 1]) + (m1[1, 1] * m1[2, 0] * m1[3, 2]) + (m1[1, 2] * m1[2, 1] * m1[3, 0])
                                            - (m1[1, 0] * m1[2, 1] * m1[3, 2]) - (m1[1, 1] * m1[2, 2] * m1[3, 0]) - (m1[1, 2] * m1[2, 0] * m1[3, 1]);
                            kofak[3, 1] = (m1[0, 0] * m1[2, 1] * m1[3, 2]) + (m1[0, 1] * m1[2, 2] * m1[3, 0]) + (m1[0, 2] * m1[2, 0] * m1[3, 1])
                                            - (m1[0, 0] * m1[2, 2] * m1[3, 1]) - (m1[0, 1] * m1[2, 0] * m1[3, 2]) - (m1[0, 2] * m1[2, 1] * m1[3, 0]);
                            kofak[3, 2] = (m1[0, 0] * m1[1, 2] * m1[3, 1]) + (m1[0, 1] * m1[1, 0] * m1[3, 2]) + (m1[0, 2] * m1[1, 1] * m1[3, 0])
                                           - (m1[0, 0] * m1[1, 1] * m1[3, 2]) - (m1[0, 1] * m1[1, 2] * m1[3, 0]) - (m1[0, 2] * m1[1, 0] * m1[3, 1]);
                            kofak[3, 3] = (m1[0, 0] * m1[1, 1] * m1[2, 2]) + (m1[0, 1] * m1[1, 2] * m1[2, 0]) + (m1[0, 2] * m1[1, 0] * m1[2, 1])
                                           - (m1[0, 0] * m1[1, 2] * m1[2, 1]) - (m1[0, 1] * m1[1, 0] * m1[2, 2]) - (m1[0, 2] * m1[1, 1] * m1[2, 0]);

                        }
                    }

                    Console.WriteLine("Determinan = " + det);
                    Console.ReadLine();

                    double[,] tps = new double[4, 4];
                    int e, f;
                    for (e = 0; e < 4; e++)
                    {
                        for (f = 0; f < 4; f++)
                        {
                            tps[e, f] = kofak[f, e];
                        }
                    }

                    double[,] invers = new double[4, 4];
                    int g, h;

                    for (g = 0; g < 4; g++)
                    {
                        for (h = 0; h < 4; h++)
                        {
                            invers[g, h] = tps[g, h] / det;
                        }
                    }

                    for (g = 0; g < 4; g++)
                    {
                        for (h = 0; h < 4; h++)
                        {
                            Console.Write("\t" + invers[g, h]);
                        }
                        Console.WriteLine();
                    }
                    Console.ReadLine();

                }


            }
        }
    }
}






1.      TAMPILAN AWAL.
2.      MATRIKS ORDO 2X2
3.     MATRIKS ORDO 3X3
4.      MATRIKS ORDO 4X4

Komentar

Postingan populer dari blog ini

Memahami kaitan antara agama,wahyu,iman,dan kebajikan umat