1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
#include "matrice.h"
//--------------------------------------------------------------
// Fonction d'allocation d'une matrice (n,n)
// Remarque : on désalloue en cas déchec en cours !
//--------------------------------------------------------------
matrice* alloc_matrice (int n,int m)
{
matrice * M = (matrice *)calloc(1,sizeof(matrice));
M->n = n;
M->m = m;
double **a;
a=(double **)calloc(n,sizeof(double *));
if (a!=NULL) {
for (int i=0; i<n; i++) {
a[i]=(double *)calloc(m,sizeof(double));
if (a[i]==NULL) {
for (int j=0; j<i; j++) free((void *)a[j]);
free((void *)a);
return NULL;
}
}
}
M->mat= a;
return M;
}
//--------------------------------------------------------------
// Fonction de désallocation d'une matrice (n,n)
//--------------------------------------------------------------
void free_matrice (matrice *M)
{
if (M->mat!=NULL) {
for (int i=0; i<M->n; i++) if (M->mat[i]!=NULL) free((void *)M->mat[i]);
free((void *)M->mat);
}
}
matrice* identite_matrice(int n){
matrice * C = alloc_matrice(n,n);
for(int i =0; i<n;i++)
C->mat[i][i] = 1;
return C;
}
matrice* add_matrice(matrice* A, matrice* B)
{
if(A->n != B->n || A->m!= B->m){
printf("erreur dans la dimension des matrices");
return NULL;
}
matrice* C= alloc_matrice(A->n,A->m);
for(int i = 0; i < C->n; i++)
for(int j = 0; j < C->m; j++)
C->mat[i][j] = A->mat[i][j] + B->mat[i][j];
return C;
}
void soust_matrice(matrice * C, matrice* A, matrice* B)
{
if(A->n != B->n || A->m!= B->m){
printf("erreur dans la dimension des matrices");
exit(0);
}
//matrice* C= alloc_matrice(A->n,A->m);
for(int i = 0; i < C->n; i++)
for(int j = 0; j < C->m; j++)
C->mat[i][j] = A->mat[i][j] - B->mat[i][j];
//return C;
}
matrice * mult_by_scal_matrice(matrice * A, double k){
if(k == 0.0){
matrice * C = alloc_matrice(A->n, A->m);
return C;
}
for(int i = 0; i<A->n;i++)
for(int j = 0; j < A->m;j++)
if(A->mat[i][j]!=0) A->mat[i][j]*=k;
return A;
}
void mult_matrice(matrice* C,matrice* A, matrice *B)
{
if(A->m != B->n)
{ printf("Erreur dans ladimensoin des matrices\n");
}
double somme;
// matrice * C = alloc_matrice(A->n,B->m);
for(int i = 0; i < A->n; i++)
for(int j = 0; j < B->m; j++){
somme = 0;
for(int k=0; k < A->m; k++)
somme += A->mat[i][k]*B->mat[k][j];
C->mat[i][j] = somme;
}
}
void trans_matrice(matrice * C,matrice* A)
{
//matrice* C= alloc_matrice(A->m,A->n);
for(int i = 0; i < A->n; i++)
for(int j = 0; j < A->m; j++)
C->mat[j][i] = A->mat[i][j];
//return C;
}
void affiche_matrice(matrice* A)
{
for(int i = 0; i < A->n; i++){
printf("\n");
for(int j = 0; j < A->m; j++)
printf("%f\t",A->mat[i][j]);
}
printf("\n\n");
}
double determinant_matrice (matrice *m)
{
if(m->n != m->n){
printf("La matrice n'est pas une matrice carrée\n");
return NULL;
}
matrice * sous_m = alloc_matrice(m->n-1, m->m-1);;
int signe=1;
double det=0;
if (m->n==1) return (m->mat[0][0]);
if(m->n==2)
return m->mat[0][0]*m->mat[1][1]-m->mat[1][0]*m->mat[0][1];
for (int j=0;j<m->n;j++){
// for(int j=0;j< m->m; j++){
mineur_matrice(sous_m,m,0,j);
signe=((j)%2 == 0)?1:-1;
det+=signe*m->mat[0][j]*determinant_matrice(sous_m);
}
free_matrice(sous_m);
free(sous_m);
return(det);
}
void mineur_matrice(matrice * min_A, matrice* A, int n,int m)
{
int ii = 0;
int jj = 0;
if(A->n == 1)
min_A->mat[0][0] = A->mat[0][0];
for(int i = 0; i < A->n; i++){
for(int j = 0; j < A->m; j++){
if(i != n && j!=m){
min_A->mat[ii][jj] = A->mat[i][j];
jj++;
}
}
if(i!=n) {ii++; jj=0;}
}
}
void trans_comatrice(matrice * CoA_t, matrice *A)
{
char signe = 1;
matrice* CoA = alloc_matrice(A->n,A->m);
matrice* min_A = alloc_matrice(A->n-1, A->m-1);
if(determinant_matrice(A)==0){
printf("La matrice est non inversible,sont déterminant est nul\n");
exit(0);
}
for(int i = 0; i < CoA->n; i++)
for(int j = 0; j < CoA->m; j++){
signe = ((i+j)%2 ==0)?1:-1;
mineur_matrice(min_A, A, i, j);
CoA->mat[i][j] = signe*determinant_matrice(min_A);
}
trans_matrice(CoA_t, CoA);
free_matrice(CoA);free(CoA);
free_matrice(min_A);free(min_A);
}
void inverse_matrice(matrice * A_1, matrice * A)
{
if(A->n != A->m){
printf("La matrice n'estpas une matrice carée, et ne peux donc etre inversée\n");
exit(0);
}
double det = determinant_matrice(A);
if(det == 0){
printf("Votre matrice est non inversible car de déterminant nul\n");
exit(0);
}
trans_comatrice(A_1,A);
for(int i = 0; i< A_1->n; i++)
for(int j = 0; j< A_1->m;j++)
A_1->mat[i][j]/=det;
} |
Partager