Share
Condensation pivotal determinants calculation method

Condensation pivotal determinants calculation method

//Calculul determinantilor cu metoda condensarii pivotale
#include <conio.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
using namespace std;
double a[10][10],b[10][10],c;
int n,i,j,t,p,q;
void main(){
//	clrscr();
	double max;
	cout<<"\nDati ordinul matricei A: n=";cin>>n;
	cout<<"\nDati matricea A:\n";
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++){
			cout<<"\na["<<i<<"]["<<j<<"]=";cin>>a[i][j];
		}
	t=1;
	while (n>=3){
		max=fabs(a[1][1]);
		p=1;
		q=1;
		for(i=2;i<=n;i++)
		for(j=2;j<=n;j++)
			if (max<fabs(a[i][j])){
				max=fabs(a[i][j]);
				p=i;
				q=j;
			}
		if (a[p][q]==0){
			cout<<"\nDeterminantul matricei A este: det(A)=0";
			exit(1);
		}
		else{
			if (p!=1){
				for(j=1;j<=n;j++){
					c=a[1][j];
					a[1][j]=a[p][j];
					a[p][j]=c;
				}
				t=-t;
			}
			if(q!=1)  {
				for(i=1;i<=n;i++){
					c=a[i][1];
					a[i][1]=a[i][q];
					a[i][q]=c;
				}
				t=-t;
			}

			for(i=3;i<=n;i++)
				for(j=1;j<=n;j++)
					a[i][j]=a[i][j]/a[1][1];
			for(i=1;i<=n-1;i++)
				for(j=1;j<=n-1;j++)
					b[i][j]=a[1][1]*a[i+1][j+1]-a[i+1][1]*a[1][j+1];
			n=n-1;
			for(i=1;i<=n;i++)
				for(j=1;j<=n;j++)
					a[i][j]=b[i][j];
		}
	}
	cout<<"\nDeterminantul matricei A este: det(A)="<<t*(a[1][1]*a[2][2]-a[1][2]*a[2][1]);
	getch();
}

25,565 total views, 2 views today