public double DeterminantRecur() { if (lignes != colonnes) { throw new Exception("Dimension error"); } if (lignes == 1) { return(tableau[0, 0]); } if (lignes == 2) { return(tableau[0, 0] * tableau[1, 1] - tableau[0, 1] * tableau[1, 0]); } double deter = 0; double signe = 1; for (int j = 0; j < colonnes; j++) { Matrice sousMat = GetDimInf(0, j); deter += signe * tableau[0, j] * sousMat.DeterminantRecur(); signe = -signe; } return(deter); }
private Matrice GetDimInf(int i0, int j0) { if (lignes > 1 && colonnes > 1) { int i1 = 0; int j1 = 0; Matrice mat = new Matrice(lignes - 1, colonnes - 1); for (int i = 0; i < lignes; i++) { if (i != i0) { j1 = 0; for (int j = 0; j < colonnes; j++) { if (j != j0) { mat[i1, j1] = tableau[i, j]; // Je copie tout sauf la ligne i0 et colonne j0 j1++; } } i1++; } } return(mat); } else { return(null); } }
public Matrice Comatrice() { if (lignes != colonnes) { throw new Exception("Dimension error"); } Matrice comat = new Matrice(lignes, colonnes); if (lignes == 1) { comat[0, 0] = tableau[0, 0]; return(comat); } double signeL = 1; for (int i = 0; i < lignes; i++) { double signeC = signeL; // changement de signe de ligne ne dépend pas de changemetn de colonne for (int j = 0; j < colonnes; j++) { Matrice sousMat = GetDimInf(i, j); comat[i, j] = signeC * sousMat.DeterminantRecur(); signeC = -signeC; } signeL = -signeL; } return(comat); }
public Matrice[] Decomposer() { Matrice[] LU = new Matrice[2]; Matrice L = new Matrice(lignes, colonnes); Matrice U = new Matrice(lignes, colonnes); LU[0] = L; LU[1] = U; // Boucle sur la diagonale de L for (int i = 0; i < lignes; i++) { L[i, i] = 1; } for (int i = 0; i < lignes; i++) { for (int j = 0; j < colonnes; j++) { if (j >= i) { double u = tableau[i, j]; for (int k = 0; k < i - 1; k++) { u -= U[k, j] * L[i, k]; } U[i, j] = u; } if (j > i) { double l = tableau[j, i]; for (int k = 0; k < i - 1; k++) { l -= U[k, i] * L[j, k]; } if (U[i, i] == 0) { Console.WriteLine("Division par 0"); } l /= U[i, i]; L[j, i] = l; } } } return(LU); }
public Matrice Transposition() { Matrice transpose = new Matrice(colonnes, lignes); for (int i = 0; i < lignes; i++) { for (int j = 0; j < colonnes; j++) { transpose[j, i] = tableau[i, j]; } } return(transpose); }
// Algorithme de Cholesky public override void Decomposer() { if (A.Lignes == A.Colonnes) { _L = new Matrice(_A.Lignes, _A.Lignes); for (int j = 0; j < A.Colonnes; j++) { double s = 0; for (int k = 0; k <= j - 1; k++) { s += (_L[j, k] * _L[j, k]); } _L[j, j] = _A[j, j] - s; if (_L[j, j] <= 0) { break; } _L[j, j] = Math.Sqrt(_L[j, j]); for (int i = j + 1; i < _A.Lignes; i++) { s = 0; for (int k = 0; i <= j - 1; k++) { s += (_L[i, k] * _L[j, k]); } _L[i, j] = (_A[i, j] - s) / _L[j, j]; } } for (int i = 0; i < _L.Lignes; i++) { for (int j = i + 1; j < _A.Lignes; j++) { _L[i, j] = 0; } } _U = _L.Transposition(); } else { throw new Exception("Matrice not squared"); } }
// Calcul de l'espérance de la colonne j d'une matrice X public static double Moyenne(Matrice X, int j0) { double moy_X = 0; double x = 0; for (int i = 0; i < X.Lignes; i++) { x = X[i, j0]; moy_X += x; } moy_X /= X.Lignes; return(moy_X); }
// Calcul de la variance de la colonne j d'une matrice X public static double Variance(Matrice X, int j0) { double moy_X = 0; double moy_X2 = 0; double x = 0; for (int i = 0; i < X.Lignes; i++) { x = X[i, j0]; moy_X += x; moy_X2 += x * x; } moy_X /= X.Lignes; moy_X2 /= X.Lignes; return(moy_X2 - moy_X * moy_X); }
public override void Decomposer() { Console.WriteLine("A = \n" + A); int n = _A.Lignes; _L = new Matrice(n, n); _U = new Matrice(n, n); Console.WriteLine("L = \n" + _L); Console.WriteLine("U = \n" + _U); Matrice PA = Pivoter(_A); Console.WriteLine("PA = \n" + PA); for (int j = 0; j < n; j++) { _L[j, j] = 1; for (int i = 0; i < j + 1; i++) { double x = 0; for (int k = 0; k < i; k++) { x += _U[k, j] * _L[i, k]; } _U[i, j] = PA[i, j] - x; } for (int i = j + 1; i < n; i++) { double x = 0; for (int k = 0; k < j; k++) { x += _U[k, j] * _L[i, k]; } _L[i, j] = (PA[i, j] - x) / _U[j, j]; } } }
// Addition entre deux matrices public Matrice Add(Matrice B) { if (this.lignes == B.lignes && this.colonnes == B.colonnes) { Matrice C = new Matrice(lignes, colonnes); for (int i = 0; i < lignes; i++) { for (int j = 0; j < colonnes; j++) { C.tableau[i, j] = tableau[i, j] + B.tableau[i, j]; } } return(C); } else { throw new Exception("Les matrices non pas les mêmes dimensions"); } }
public Matrice Permutation() { Matrice P = new Matrice(lignes, colonnes); int[] ancienMax = new int[colonnes]; int indice = 0; for (int j = 0; j < colonnes; j++) { ancienMax[j] = -1; // -1 pour dire que la case est vide } for (int j = 0; j < colonnes; j++) { int ligneMax = getMaxSurColonne(j, ancienMax); ancienMax[indice] = ligneMax; indice++; P.tableau[ligneMax, j] = 1; } return(P); }
public Matrice Inverse() { if (lignes == colonnes) { double det = this.DeterminantRecur(); if (det != 0) { Matrice inverse = this.Comatrice().Transposition(); inverse.ScalarMult(1.0 / det); return(inverse); } else { return(null); } } else { throw new Exception("Dimensions incorrectes pour la multiplication"); } }
public Matrice Mult(Matrice B) { if (this.colonnes == B.lignes) { Matrice C = new Matrice(this.lignes, B.colonnes); for (int i = 0; i < C.lignes; i++) { for (int j = 0; j < C.colonnes; j++) { double res = 0; for (int k = 0; k < colonnes; k++) { res += tableau[i, k] * B.tableau[k, j]; } C.tableau[i, j] = res; } } return(C); } throw new Exception("Dimensions incorrectes pour la multiplication"); }
private Matrice Pivoter(Matrice A) { Matrice Id = new Matrice(A.Lignes, A.Lignes); for (int i = 0; i < A.Lignes; i++) { Id[i, i] = 1; } for (int i = 0; i < A.Lignes; i++) { double max = A[i, i]; int ligne = i; for (int j = i; j < A.Colonnes; j++) { if (A[j, i] > max) { max = A[j, i]; ligne = j; } if (i != ligne) { for (int k = 0; k < Id.Lignes; k++) { double tmp = Id[i, k]; Id[i, k] = Id[ligne, k]; Id[ligne, k] = tmp; } } } } return(Id); }
// Calcul de la corrélation entre deux colonnes public static double Correlation(Matrice rendements, int j, int k) { double moyenne_j = 0; double moyenne_k = 0; for (int i = 0; i < rendements.Lignes; i++) { moyenne_j += rendements[i, j]; moyenne_k += rendements[i, k]; } moyenne_j /= rendements.Lignes; moyenne_k /= rendements.Lignes; double ecart_jk = 0; double variance_jk = 0; double variance_j = 0; double variance_k = 0; for (int i = 0; i < rendements.Lignes; i++) { double diff_j = rendements[i, j] - moyenne_j; double diff_k = rendements[i, k] - moyenne_k; ecart_jk += diff_j * diff_k; variance_j += diff_j * diff_j; variance_k += diff_k * diff_k; } variance_jk = Math.Sqrt(variance_j * variance_k); return(ecart_jk / variance_jk); }
public Pivot(Matrice A) : base(A) { }
public Decomposition(Matrice A) { this._A = A; }
// Calcul de l'écart-type de la colonne j d'une matrice X public static double EcartType(Matrice X, int j0) { return(Math.Sqrt(Variance(X, j0))); }
public Cholesky(Matrice A) : base(A) { }