Advertisement
ntamas

lineáris egyenletrendszer megoldása

Jun 13th, 2014
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace cramer
  7. {
  8.   class Program
  9.   {
  10.     static void sorcsere(double[,] tomb, ref int sorindex, int k)
  11.     {
  12.       double temp;
  13.       for (int j = 0; j < tomb.GetLength(1); j++)
  14.       {
  15.         temp = tomb[sorindex,j];
  16.         tomb[sorindex, j] = tomb[k, j];
  17.         tomb[k, j] = temp;
  18.       }
  19.     }
  20.     static void oszlopcsere(double[,] tomb, ref int oszlopindex, int k)
  21.     {
  22.       double temp;
  23.       for (int i = 0; i < tomb.GetLength(0); i++)
  24.       {
  25.         temp = tomb[i, oszlopindex];
  26.         tomb[i, oszlopindex] = tomb[i, k];
  27.         tomb[i, k] = temp;
  28.       }
  29.     }
  30.     static void abszelemkereses(double[,] tomb, double max, ref int sorindex, ref int oszlopindex, int k)
  31.     {
  32.       for (int i = k+1; i < tomb.GetLength(0); i++)
  33.       {
  34.         for (int j = k; j < tomb.GetLength(1); j++)
  35.         {
  36.           if (max == Math.Abs(tomb[i, j]))
  37.           {
  38.             sorindex = i;
  39.             oszlopindex = j;
  40.           }
  41.         }
  42.       }
  43.     }
  44.     static double maxabszkereses(double[,] tomb, int k)
  45.     {
  46.       double max = 0;
  47.       for (int i = k+1; i < tomb.GetLength(0); i++)
  48.       {
  49.         for (int j = k; j < tomb.GetLength(1); j++)
  50.         {
  51.           if (max < Math.Abs(tomb[i, j]))
  52.           {
  53.             max = Math.Abs(tomb[i, j]);
  54.           }
  55.         }
  56.       }
  57.       return max;
  58.     }
  59.     static void visszatoltes(double[,] tomb1, double[,] tomb2)
  60.     {
  61.       for (int i = 0; i < tomb1.GetLength(0); i++)
  62.       {
  63.         for (int j = 0; j < tomb1.GetLength(1); j++)
  64.         {
  65.           tomb1[i, j] = tomb2[i, j];
  66.         }
  67.       }
  68.     }
  69.     static void csere(double[,] tomb, double[] szabadtagok, int oszlopindex)
  70.     {
  71.       for(int i = 0; i < tomb.GetLength(1); i++)
  72.       {
  73.         tomb[i,oszlopindex-1] = szabadtagok[i];
  74.       }
  75.     }
  76.     static double determinans(double[,] tomb)
  77.     {
  78.       double szorzat = 1;
  79.       for (int i = 0; i < tomb.GetLength(1); i++)
  80.       {
  81.         szorzat *= tomb[i, i];
  82.       }
  83.       return szorzat;
  84.     }
  85.     static void kiir(double[,] tomb)
  86.     {
  87.       for (int i = 0; i < tomb.GetLength(0); i++)
  88.       {
  89.         for (int j = 0; j < tomb.GetLength(1); j++)
  90.         {
  91.           Console.Write("{0,6:0.00} ", tomb[i, j]);
  92.         }
  93.         Console.WriteLine();
  94.       }
  95.     }
  96.     static int kinullazas(double[,] tomb, ref int sorindex, ref int oszlopindex)
  97.     {
  98.       double hanyados, tarolt;
  99.       int elojel = 1;
  100.       for (int k = 0; k < tomb.GetLength(0) - 1; k++)
  101.       {
  102.         abszelemkereses(tomb, maxabszkereses(tomb, k), ref sorindex, ref oszlopindex, k);
  103.         if(tomb[sorindex ,oszlopindex] != 0)
  104.         {
  105.           if(k != sorindex)
  106.           {
  107.             elojel *= -1;
  108.             sorcsere(tomb, ref sorindex, k);
  109.           }
  110.           if(k != oszlopindex)
  111.           {
  112.             elojel *= -1;
  113.             oszlopcsere(tomb, ref oszlopindex, k);  
  114.           }
  115.           for (int i = k + 1; i < tomb.GetLength(0); i++)
  116.           {
  117.             tarolt = tomb[i, k];
  118.             for (int j = k; j < tomb.GetLength(1); j++)
  119.             {
  120.               hanyados = tarolt / tomb[k, k];
  121.               tomb[i, j] = tomb[i, j] + tomb[k, j] * -(hanyados);
  122.             }
  123.           }
  124.         }
  125.       }
  126.       return elojel;
  127.     }
  128.     static void Main(string[] args)
  129.     {
  130.       int oszlopindex, elojel;
  131.       int sor=0, oszlop=0;
  132.       double[,] feltoltheto = new double[4, 4];
  133.       double[,] tomb1 = new double[4, 4] { // cserére szánt
  134.       {1, -2, 1, 0},
  135.       {2, 1, 0, -1},
  136.       {0, -1, 1, 2},
  137.       {-1, 1, -1, 3}
  138.       };
  139.       double[,] tomb2 = new double[4, 4] { // eredeti
  140.       {1, -2, 1, 0},
  141.       {2, 1, 0, -1},
  142.       {0, -1, 1, 2},
  143.       {-1, 1, -1, 3}
  144.       };
  145.       double[] szabadtagok = new double[4] {4, 3, 1, -7};
  146.       double[] determinansok = new double[5];
  147.       double[] megoldasok = new double[4];
  148.       kiir(tomb1);
  149.       Console.WriteLine();
  150.       elojel = kinullazas(tomb1, ref sor, ref oszlop);
  151.       determinansok[0] = elojel*determinans(tomb1);
  152.       visszatoltes(tomb1, tomb2);
  153.       for (oszlopindex = 1; oszlopindex < tomb1.GetLength(0)+1; oszlopindex++)
  154.       {
  155.         csere(tomb1, szabadtagok, oszlopindex);
  156.         elojel = kinullazas(tomb1, ref sor, ref oszlop);
  157.         kiir(tomb1);
  158.         Console.WriteLine();
  159.         determinansok[oszlopindex] = elojel*determinans(tomb1);
  160.         visszatoltes(tomb1, tomb2);
  161.       }
  162.       for (int i = 0; i < tomb2.GetLength(0); i++)
  163.       {
  164.         megoldasok[i] = determinansok[i+1] / determinansok[0];
  165.         Console.WriteLine("x{0}= {1}", i+1, megoldasok[i]);
  166.       }
  167.       Console.ReadKey();
  168.     }
  169.   }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement