Guest User

GaussJ_procedure_from_Numerical_Recipes

a guest
Mar 1st, 2026
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.63 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3. using ExtendedNumerics;
  4. namespace NamespaceName
  5. {
  6.     public class ClassName
  7.     {
  8.         public static void GaussJ(BigRational[,] a,out byte error)
  9.         {
  10.             /*
  11.              * Matrix inversion by Gauss-Jordan elimination
  12.              * The input matrix a[0..n-1,0..n-1] has n by n elements
  13.              * On output, a is replaced by its matrix inverse
  14.              */
  15.             if(a.GetLength(0) != a.GetLength(1))
  16.             {
  17.                 error = 2;
  18.                 return;
  19.             }
  20.             BigRational big,dum,pivinv;
  21.             int n,icol,irow;
  22.             int[] indxc,indxr,ipiv;
  23.             /*The integer arrays ipiv[0..n-1],indxr[0..n-1] and indxc[0..n-1]
  24.              * are used for bookkeeping on the pivoting
  25.              */
  26.             n = a.GetLength(0);
  27.             error = 0;
  28.            
  29.             indxc = new int[n];
  30.             indxr = new int[n];
  31.             ipiv = new int[n];
  32.             for(int j=0;j<n;j++) ipiv[j] = 0;
  33.             for(int i=0;i<n;i++){   //This is main loop over columns to be reduced
  34.                 big = 0;
  35.                 icol = -1;
  36.                 irow = -1;
  37.                 for(int j=0;j<n;j++) //This is tho outer loop of the searchh for a povot element
  38.                     if(ipiv[j] != 1)
  39.                         for(int k=0;k<n;k++)
  40.                             if(ipiv[k]==0)
  41.                                 if(BigRational.Abs(a[j,k]) >= big){
  42.                                     big = BigRational.Abs(a[j,k]);
  43.                                     irow = j;
  44.                                     icol = k;
  45.                                 }
  46.                                 else if(ipiv[k] > 1){
  47.                                     Console.WriteLine("Pause 1 in GAUSSJ - singular matrix");
  48.                                     Console.ReadKey();
  49.                                     error = 1;
  50.                                     return;
  51.                                 }
  52.                             ipiv[icol]++;
  53.                             /*
  54.                              * We now have the pivot element,so we interchange rows,if needed to put the pivot element on the
  55.                              * diagonal The columns are not phisically interchanged, only relabeled: indxc[i] , the column of the
  56.                              * ith pivot element, is the ith column that is reduced, while indxr[i]  is the row in which that pivot
  57.                              * element was originally located. If indxr[i] != indxc[i] there is implied column interchange. With
  58.                              * this form of bookkeeping the inverse matrix will be scrambled by columns
  59.                              */
  60.                             if(irow != icol){
  61.                                 for(int l = 0;l<n;l++){
  62.                                     dum = a[irow,l];
  63.                                     a[irow,l] = a[icol,l];
  64.                                     a[icol,l] = dum;
  65.                                 }
  66.                             }
  67.                             indxr[i] = irow; //We are now ready to divide the pivot row  by the pivot element, located at irow and icol  
  68.                             indxc[i] = icol;
  69.                             if(a[icol,icol] == 0){
  70.                                 Console.WriteLine("Pause 2 in GAUSSJ - singular matrix");
  71.                                 Console.ReadKey();
  72.                                 error = 1;
  73.                                 return;
  74.                             }
  75.                 pivinv = 1/a[icol,icol];
  76.                 a[icol,icol] = 1;
  77.                 for(int l=0;l<n;l++)
  78.                     a[icol,l] *= pivinv;
  79.                 for(int ll=0;ll<n;ll++) //Next we reduce rows...
  80.                     if(ll != icol){     // ...except fotr the pivot one, of course
  81.                         dum = a[ll,icol];
  82.                         a[ll,icol] = 0;
  83.                         for(int l=0;l<n;l++)
  84.                             a[ll,l] -= a[icol,l]*dum;
  85.                         }
  86.                 }
  87.                 /*
  88.                  * This is the end of the main loop over columns of reduction. It only remains to unscramble the solution
  89.                  * in view of the column interchanges. We do this by interchanging pairs of columns in reverse order
  90.                  * that permutation was built up  
  91.                  */
  92.                 for(int l=n-1;l>=0;l--)
  93.                     if(indxr[l] != indxc[l])
  94.                         for(int k=0;k<n;k++){
  95.                             dum = a[k,indxr[l]];
  96.                             a[k,indxr[l]] = a[k,indxc[l]];
  97.                             a[k,indxc[l]] = dum;
  98.                         }
  99.            
  100.         }//And we are done
  101.        
  102.         public static void PrintMatrix(BigRational[,] a)
  103.         {
  104.             int m = a.GetLength(0),n = a.GetLength(1);
  105.             for(int i = 0;i < m;i++)
  106.             {
  107.                 for(int j = 0;j < m;j++)
  108.                     Console.Write("{0} , ",a[i,j].GetImproperFraction());
  109.                 Console.WriteLine();
  110.             }
  111.             Console.WriteLine();
  112.                
  113.         }
  114.        
  115.         public static void Main(string[] args)
  116.         {
  117.             char esc;
  118.             int n;
  119.             BigRational[,] a;
  120.             byte error;
  121.             do
  122.             {
  123.                 Console.WriteLine("Odwracanie macierzy metodą Gaussa-Jordana z pełnym wyborem elementu głównego");
  124.                 Console.WriteLine("Program korzysta z procedury zamieszczonej w Numerical Recipes in Pascal przetłumaczonej na C#");
  125.                 Console.WriteLine("Podaj stopien odwracanej macierzy");
  126.                 n = Convert.ToInt32(Console.ReadLine());
  127.                 a = new BigRational[n,n];
  128.                 Console.WriteLine("Wczytaj macierz do odwrócenia");
  129.                 for(int i=0;i<n;i++)
  130.                 {
  131.                     Console.WriteLine("Wczytaj wiersz {0} ",i+1);
  132.                     for(int j=0;j<n;j++)
  133.                     {
  134.                         Console.Write("a[{0},{1}] = ",i+1,j+1);
  135.                         a[i,j] = BigRational.Parse(Console.ReadLine());
  136.                     }
  137.                 }
  138.                 Console.WriteLine();
  139.                 Console.WriteLine("Wczytana macierz :");
  140.                 PrintMatrix(a);
  141.                 GaussJ(a,out error);
  142.                 if(error == 0)
  143.                 {
  144.                     Console.WriteLine("Macierz odwrotna to:");
  145.                     PrintMatrix(a);
  146.                 }
  147.                 esc = (char)Console.ReadKey(true).Key;
  148.             }
  149.             while(esc != (char)ConsoleKey.Escape);
  150.         }
  151.     }
  152. }
  153.  
Advertisement
Add Comment
Please, Sign In to add comment