IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Mathématiques Discussion :

[Débutant] Calculer le déterminant d'une matrice


Sujet :

Mathématiques

  1. #1
    Membre actif
    Homme Profil pro
    Analyste/développeur Java EE
    Inscrit en
    Janvier 2005
    Messages
    376
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Belgique

    Informations professionnelles :
    Activité : Analyste/développeur Java EE

    Informations forums :
    Inscription : Janvier 2005
    Messages : 376
    Points : 271
    Points
    271
    Par défaut [Débutant] Calculer le déterminant d'une matrice
    Bonjour à tous,
    j'ai développé un programme qui effectue des opérations sur les matrices. Et dans la plupart des opérations, j'ai besoin de calculer le déterminant des matrices. Au début, j'ai cherché sans trouver d'algorithme et je l'ai programmé au cas par cas (si taille=1, si taille=2,...). Si vous pouviez m'aider à trouver un algorithme qui le ferait sans passer par le cas par cas, ce serait génial, ça offirait encore plus de possibilités au programme.

    Merci d'avance pour vos lumières.

    P.S.: je programme en java.

  2. #2
    Membre confirmé
    Profil pro
    Enseignant
    Inscrit en
    Avril 2004
    Messages
    440
    Détails du profil
    Informations personnelles :
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Avril 2004
    Messages : 440
    Points : 451
    Points
    451
    Par défaut
    Salut, tu peux essayer d'implanter la formule brute: http://jellevy.yellis.net/Classes/Su...cours_det.html, ou bien peut-être essayer les formules de développement de déterminants suivant les lignes ou les colonnes : http://www.meca.unicaen.fr/Enseignem...00000000000000...
    A mon avis ça peut être vraiment plus rapide car ça décompose jusqu'à la dimension que tu veux (donc jusqu'à la dimension 3x3 où la formule est connue ) et ça doit s'implanter de façon récursive...

    A+

  3. #3
    Membre actif
    Homme Profil pro
    Analyste/développeur Java EE
    Inscrit en
    Janvier 2005
    Messages
    376
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Belgique

    Informations professionnelles :
    Activité : Analyste/développeur Java EE

    Informations forums :
    Inscription : Janvier 2005
    Messages : 376
    Points : 271
    Points
    271
    Par défaut
    Merci pour votre réponse, pour le moment, mon programme calcule les déterminant de matrice jusqu'à 3x3 (matrice carrées) par la règle de Sarrus. Je vois sur les pages que vous m'avez passées, on parle de la règle de Laplace (encore appellée celle des cofacteurs). Mais cette méthode ferait appel à la règle de Sarrus et me permettrai de calculer les matrices jusque 4x4. Et j'essaye de trouver une méthode qui me permettrait d'aller jusqu'à nxn.

    Il y a surement moyen avec la règle de Sarrus mais je ne parviens pas à trouver l'algorithme qui me permettrait de le faire sans traiter au cas par cas.

    Merci d'avance pour votre aide

  4. #4
    Membre confirmé
    Profil pro
    Enseignant
    Inscrit en
    Avril 2004
    Messages
    440
    Détails du profil
    Informations personnelles :
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Avril 2004
    Messages : 440
    Points : 451
    Points
    451
    Par défaut
    Eh bien simplement, pour une matrice nxn, tu décomposes en (n-1)x(n-1) via la décomposition selon une ligne ou colonne, et tu recommences sur tes matrices (n-1)x(n-1) pour les décomposer en matrices (n-2)x(n-2) et ainsi de suite jusqu'à des matrices 3x3.

    En fait c'est une logique récursive, c'est la même méthode que pour une matrice 4x4, mais généralisée...

    A+

  5. #5
    Membre actif
    Homme Profil pro
    Analyste/développeur Java EE
    Inscrit en
    Janvier 2005
    Messages
    376
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Belgique

    Informations professionnelles :
    Activité : Analyste/développeur Java EE

    Informations forums :
    Inscription : Janvier 2005
    Messages : 376
    Points : 271
    Points
    271
    Par défaut
    J'avoue ne pas comprendre
    Pourriez-vous m'expliquez sous la forme de boucles,...?

    Un grand merci d'avance

  6. #6
    Rédacteur
    Avatar de Laurent Gomila
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2003
    Messages
    10 651
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2003
    Messages : 10 651
    Points : 15 920
    Points
    15 920
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
        |a b c d|           |f g h|           |e g h|           |e f h|           |e f g|
    det |e f g h| = a * det |j k l| - b * det |i k l| + c * det |i j l| - d * det |i j k|
        |i j k l|           |n o p|           |m o p|           |m n p|           |m n o|
        |m n o p|
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Fonction determinant (Matrice M)
    debut
       det = 0
       pour i de 1 à M.taille
          det += (-1)^(i + 1) * M[1][i] * determinant(sous_matrice(M, 1, i))
       fin pour
       renvoyer det
    fin
     
    // sous_matrice(M, i, j) renvoie la sous-matrice de M dont on a retiré la i-ème ligne et la j-ième colonne

  7. #7
    Membre habitué Avatar de maleaume
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2005
    Messages
    93
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mai 2005
    Messages : 93
    Points : 131
    Points
    131
    Par défaut
    Le code que je t'envoi permet d'inverser ou d ecalculer le det de n'importe qu'elle matrice carré.. c'est pas ce qu'il y a d eplus optimise rmais il marche bien

    tiens voici le fichier .h
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    #ifndef __MATRICE_H__
    #define __MATRICE_H__
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
     
    #define DIM 2
     
    #ifndef __PI__
    #define __PI__
    #define PI 3.14159265358979
    #endif
     
    typedef struct Matrice{
    	int n;
    	int m;
    	double ** mat;
    }matrice;
     
    void mineur_matrice(matrice * min_A,matrice* A, int n,int m);
    void inverse_matrice(matrice * A_1, matrice * A);
    void trans_comatrice(matrice * CoA, matrice *A);
    double determinant_matrice (matrice *m);
    matrice * mineur_matrice(matrice* A, int n,int m);
    void affiche_matrice(matrice* A);
    void  trans_matrice(matrice * C,matrice* A);
    void mult_matrice(matrice * C,matrice* A, matrice *B);
    void soust_matrice(matrice* C,matrice* A, matrice* B);
    matrice* add_matrice(matrice* A, matrice* B);
    matrice* alloc_matrice (int n,int m);
    matrice* identite_matrice(int n);
    matrice * mult_by_scal_matrice(matrice * A, double k);
    void free_matrice(matrice *);
    #endif
    et le fichier cpp
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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;
     
    }

  8. #8
    Membre actif
    Homme Profil pro
    Analyste/développeur Java EE
    Inscrit en
    Janvier 2005
    Messages
    376
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Belgique

    Informations professionnelles :
    Activité : Analyste/développeur Java EE

    Informations forums :
    Inscription : Janvier 2005
    Messages : 376
    Points : 271
    Points
    271
    Par défaut
    Merci à vous, ça résoud le problème

    Encore un grand merci

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Lazarus] Calcul du déterminant d'une matrice
    Par ElodyE dans le forum Lazarus
    Réponses: 3
    Dernier message: 26/01/2015, 09h22
  2. Réponses: 3
    Dernier message: 13/09/2007, 18h26
  3. Inversion et déterminant d'une matrice
    Par coline dans le forum Algorithmes et structures de données
    Réponses: 16
    Dernier message: 23/06/2006, 09h01
  4. calcul du determinant d'une matrice
    Par gautret dans le forum Algorithmes et structures de données
    Réponses: 12
    Dernier message: 17/03/2006, 21h30
  5. [Matrices] Comment calculer le Déterminant d'une matrice 4x4
    Par cyber_N dans le forum Algorithmes et structures de données
    Réponses: 70
    Dernier message: 19/08/2005, 15h47

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo