Classe matrice

Description

Mon code est une classe permettant de gérer des matrices.
Plusieurs constructeurs possibles. Initialisation à la main ou à partir d'un tableau de double.
Opérateurs redéfinis: + et * (produit matriciel et scalaire)
Methode ToString redéfinie.
avec les methodes et propriétés définies, on peut obtenir (pêle-mêle):
-La dimension
-Comatrice
-Déterminant
-Inverse (plus propriétés IsInversible pour tester si inversible)
-(test si carrée)
-Transposée
je n'ai pas trouvé d'algo simple pour le rang.

J'espère que cette source vous plaira, c'est mon premier code en C#. Je viens du C++ (avec un niveau à peine de débutant).

Deux projets SharpDevelop sont inclus dans le Zip : Le prohet de la class (Maths) et un projet de Test.

Source / Exemple :


/*

  • Created by SharpDevelop.
  • User: Benoît
  • Date: 09/10/2005
  • Time: 22:37
  • /
using System; namespace Freeben.Maths { /// <summary> /// Classe Matrices: Classe permettant de manipuler des matrices (addition, produit matriciel, transposition, inversion, ...) /// </summary> public class Matrice { //<attributs> private double[,] _matrice; private int _nbLignes; private int _nbColonnes; //</attributs> //<Constructeurs> // Cas général: public Matrice( int n, int p ) { this._matrice = new double[n,p]; this._nbLignes = n; this._nbColonnes = p; } // Copie d'une autre matrice: public Matrice ( Matrice originale ) { int n = originale._matrice.GetLength(0); int p = originale._matrice.GetLength(1); this._matrice = new double[n,p]; this._nbColonnes = originale._nbColonnes; this._nbLignes = originale._nbLignes; Initialise( originale._matrice ); } // Cas initialisée par un tableau: public Matrice( double[,] tableau ) { int n = tableau.GetLength(0); int p = tableau.GetLength(1); this._matrice = new double[n,p]; this._nbLignes = n; this._nbColonnes = p; Initialise( tableau ); } // Matrice Carrée: public Matrice(int n) { this._matrice = new double[n,n]; this._nbLignes = n; this._nbColonnes = n; } //</Constructeurs> //<indexeur> public double this[int n, int p] { get { return _matrice[n,p]; } set { _matrice[n,p] = value; } } //</indexeur> //<Propriétés> public string Length { get { int n = _matrice.GetLength(0); int p = _matrice.GetLength(1); string length = "("+n+","+p+")"; return length; } } public Matrice Transpose { get { double[,] TableauTemporaire = new double[_nbColonnes,_nbLignes]; for ( int j = 0 ; j < _nbLignes ; j++ ) { for ( int i = 0 ; i < _nbColonnes ; i++ ) { TableauTemporaire[i,j] = _matrice[j,i]; } } return new Matrice( TableauTemporaire ); } } public double Determinant { get { double det = 0; Matrice B; //Conditions d'arrêt if ( this._nbLignes == 1 ) return this[0,0]; if ( this._nbLignes == 2 ) return ( this[0,0]*this[1,1] - this[0,1]*this[1,0] ); //Traitement par récursivité for ( int j = 0 ; j < this._nbLignes ; j++ ) { B = this.SousMatrice( 0 , j ); if ( j % 2 == 0 ) { det += this[0,j]*B.Determinant; } else { det += -1*this[0,j]*B.Determinant; } } return det; } } public Matrice Comatrice { get { Matrice B = new Matrice( this._nbLignes , this._nbColonnes ); Matrice S; for ( int i = 0 ; i < B._nbColonnes ; i++ ) { for ( int j = 0 ; j < B._nbColonnes ; j++ ) { S = this.SousMatrice( i , j ); if( ( i + j ) % 2 == 0 ) { B[i,j] = S.Determinant; } else { B[i,j] = -1 * S.Determinant; } } } return B; } } public Matrice Inverse { get { double det = this.Determinant; Matrice t_Comatrice = this.Comatrice.Transpose; Matrice Inverse; Inverse = t_Comatrice * (1/det); return Inverse; } } public double Trace { get { double Trace = 0; try { if ( this._nbLignes == this._nbColonnes ) { for ( int i = 0 ; i < this._nbLignes ; i++ ) { Trace += this[i,i]; } return Trace; } else { throw new Exception( "Impossible de calculer la trace du matrice non carrée" ); } } catch ( Exception e ) { Console.Error.WriteLine( "" + e ); return Trace; } } } public bool IsCarree { get { if ( this._nbLignes == this._nbColonnes ) { return true; } else { return false; } } } public bool IsInversible { get { if ( this.Determinant != 0 ) { return true; } else { return false; } } } //</Propriétés> //<Méthodes> public override string ToString () { string liste = ""; for ( int i = 0; i < _nbLignes; i++ ) { liste += "|"; for (int j = 0; j < _nbColonnes; j++) { liste+=" "+_matrice[i,j]; } liste += " |\n"; } return liste; } public void Initialise (double[,] tableau) { bool OK = false; for (int i = 0; i <= 1; i++) { if ( this._matrice.GetLength(0) == tableau.GetLength(0) ) { OK = true; } else { break; } } try { if ( OK ) { _matrice = tableau; } else { throw new Exception("La dimension des données fournies ne correspond pas à la taille de la matrice."); } } catch ( Exception e ) { Console.Error.WriteLine( ""+e ); } } public void Initialise ( string NomMatrice) { Console.WriteLine( "---\nInitialisation de la matrice " + NomMatrice + " ( taille = " + this.Length + " )" ); for ( int i = 0 ; i < this._nbLignes ; i++ ) { for ( int j = 0 ; j < this._nbColonnes ; j++ ) { Console.Write ( NomMatrice + "[" + (i+1) + "," + (j+1) + "]=" ); this[i,j] = Double.Parse( Console.ReadLine() ); } } } public Matrice SousMatrice ( int ib , int jb ) { Matrice B = new Matrice( this._nbLignes - 1 , this._nbColonnes - 1 ); int ir = 0, jr = 0; for ( int i = 0 ; i < this._nbLignes ; i++ ) { for ( int j = 0 ; j < this._nbColonnes ; j++ ) { if( i != (ib) && j != (jb) ) { B[ir,jr]=this[i,j]; if ( jr < B._nbLignes-1 ) jr++; else { jr = 0; ir++; } } } } return B; } //</Méthodes> //<Opérateurs> public static Matrice operator +( Matrice A , Matrice B ) { try { if ( A.Length == B.Length ) { Matrice C = new Matrice( A._nbLignes , A._nbColonnes ); for ( int i = 0 ; i < A._nbLignes ; i++ ) { for (int j = 0 ; j < A._nbColonnes ; j++ ) { C[i,j] = A[i,j] + B[i,j]; } } return C; } else { throw new Exception( "Impossible d'additionner des matrices de dimensions différentes" ); } } catch ( Exception e ) { Console.Error.WriteLine( "" + e ); Matrice C = new Matrice( 1 , 1 ); return C; } } public static Matrice operator -( Matrice A , Matrice B ) { try { if ( A.Length == B.Length ) { Matrice C = new Matrice( A._nbLignes , A._nbColonnes ); for ( int i = 0 ; i < A._nbLignes ; i++ ) { for (int j = 0 ; j < A._nbColonnes ; j++ ) { C[i,j] = A[i,j] - B[i,j]; } } return C; } else { throw new Exception( "Impossible de soustraire des matrices de dimensions différentes" ); } } catch ( Exception e ) { Console.Error.WriteLine( "" + e ); Matrice C = new Matrice( 1 , 1 ); return C; } } public static Matrice operator *( Matrice A , Matrice B ) { try { if ( A._nbColonnes == B._nbLignes ) { Matrice C = new Matrice( A._nbLignes , B._nbColonnes ); for ( int i = 0 ; i < A._nbLignes ; i++ ) { for ( int j = 0 ; j < B._nbColonnes ; j++ ) { for ( int k = 0 ; k < A._nbColonnes ; k++ ) { C[i,j] += A[i,k]*B[k,j]; } } } return C; } else { throw new Exception("Impossible de multiplier les matrices, les dimensions ne correspondent pas"); } } catch ( Exception e ) { Console.Error.WriteLine( ""+e ); Matrice C = new Matrice(1); return C; } } public static Matrice operator *( double n , Matrice A ) { Matrice B = new Matrice( A ); for ( int i = 0 ; i < A._nbLignes ; i++ ) { for (int j = 0 ; j < A._nbColonnes ; j++ ) { B[i,j] = n * A[i,j]; } } return B; } public static Matrice operator *( Matrice A , double n ) { Matrice B; B = n * A; return B; } //</Opérateurs> } }

Conclusion :


Ceci est la premiere classe de tout une lib que j'ai en projet pour gérer différents éléments mathématiques, aussi j'aimerais avoir le plus de feedback possible afin de pouvoir optimiser mon code, chasser les bugs. Vu que je suis débutant n'hésiter pas à me dire tout ce que vous pouvez voir afin que je m'améliore. Merci :)

Je n'ai pas commenté le code, c'est la première chose que je vais faire, puis je reposterais le code commenté (qui sait pk pas une doc xml ;) ).

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.

Du même auteur (freeben666)