gauss jordan elim

numerical
mail@pastecode.io avatar
unknown
plain_text
20 days ago
4.3 kB
4
Indexable
Never
#include<bits/stdc++.h>
using namespace std;

int main(){
    int n;	cin >> n;
	float arr[10][10],x[10];
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n+1;j++){
			cin >> arr[i][j];
		}
	}
	float ratio;
	for(int i=1;i<=n-1;i++){
		for(int j=i+1;j<=n;j++){
			ratio = arr[j][i]/arr[i][i];
			for(int k=1;k<=n+1;k++){
				arr[j][k] = arr[j][k]-ratio*arr[i][k];
			}
		}
	}

	x[n] = arr[n][n+1]/arr[n][n];
	for(int i=n-1;i>=1;i--){
		x[i] = arr[i][n+1];
		for(int j=i+1;j<=n;j++){
			x[i] = x[i]-arr[i][j]*x[j];
		}
		x[i] = x[i]/arr[i][i];
	}

	for(int i=1;i<=n;i++){
		cout << x[i] << endl;
	}

    for(int i=0;i<n;i++){
      for(int j=0;j<=n;j++){
         cout<<arr[i][j]<<" "; 
      }
      cout<<endl;
    }
}


===========================================
==========================================

jordan


#include <bits/stdc++.h>
using namespace std;

int main() {
    // Fast input/output
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;  // Number of variables
    double arr[10][11], x[10];  // Augmented matrix (n x (n+1))

    // Input augmented matrix
    for (int i = 0; i < n; i++) {
        for (int j = 0; j <= n; j++) {
            cin >> arr[i][j];
        }
    }

    // Applying Gauss-Jordan elimination
    for (int i = 0; i < n; i++) {
        // Step 1: Make the diagonal element 1 (Pivot normalization)
        double pivot = arr[i][i];
        for (int j = 0; j <= n; j++) {
            arr[i][j] /= pivot;  // Normalize the pivot row
        }

        // Step 2: Make all elements in the current column (except the pivot) zero
        for (int j = 0; j < n; j++) {
            if (i != j) {
                double factor = arr[j][i];
                for (int k = 0; k <= n; k++) {
                    arr[j][k] -= factor * arr[i][k];  // Subtract the pivot row multiplied by factor
                }
            }
        }
    }

    // The augmented matrix should now have identity on the left, and the solution on the right.
    // Extract the solution from the last column
    for (int i = 0; i < n; i++) {
        cout << fixed << setprecision(6) << arr[i][n] << endl;  // Set precision to 6 decimal places
    }

    for(int i=0;i<n;i++){
      for(int j=0;j<=n;j++){
         cout<<arr[i][j]<<" "; 
      }
      cout<<endl;
    }

    return 0;
}

=================================================
====================================================================

===================================

inverse 

#include <bits/stdc++.h>
// #include<iostream>
// #include<math.h>
// #include<algorithm>
// #include<string>
// #include<vector>
using namespace std;
#define ll long long int

#include <bits/stdc++.h>
using namespace std;

int main() {
    // Fast input/output
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;  // Size of the matrix (n x n)
    double arr[10][20];  // Augmented matrix of size (n x 2n)

    // Input the matrix and augment it with the identity matrix
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> arr[i][j];  // Input the original matrix
        }
        for (int j = n; j < 2 * n; j++) {
            arr[i][j] = (i == j - n) ? 1 : 0;  // Create the identity matrix on the right side
        }
    }

    // Performing Gauss-Jordan elimination
    for (int i = 0; i < n; i++) {
        // Make the diagonal element 1 (Pivot normalization)
        double pivot = arr[i][i];
        for (int j = 0; j < 2 * n; j++) {
            arr[i][j] /= pivot;
        }

        // Make the other elements in the current column 0
        for (int j = 0; j < n; j++) {
            if (i != j) {
                double factor = arr[j][i];
                for (int k = 0; k < 2 * n; k++) {
                    arr[j][k] -= factor * arr[i][k];
                }
            }
        }
    }

    // Output the inverse matrix (right side of the augmented matrix)
    cout << fixed << setprecision(6);
    for (int i = 0; i < n; i++) {
        for (int j = n; j < 2 * n; j++) {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}
Leave a Comment