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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | #include #include using namespace std; // ******* Structures ******* struct Matrice { float *tab; int nl, nc; }; struct Vecteur { float *tab; int n; }; // ******* Fonctions pour Matrice ******* float get(Matrice *mat, int ligne, int col) { if (ligne >= 0 && ligne < mat->nl && col >= 0 && col < mat->nc) return mat->tab[ligne * mat->nc + col]; else { cout << "Erreur [get]: indice en dehors des bornes!" << endl; cout << "Matrice: " << mat->nc << " colonnes, " << mat->nl << " lignes." << endl; cout << "Requete: ligne " << ligne << ", colonne " << col << endl; } return 0; } void set(Matrice *mat, int ligne, int col, float val) { if (ligne >= 0 && ligne < mat->nl && col >= 0 && col < mat->nc) mat->tab[ligne * mat->nc + col] = val; else { cout << "Erreur [set]: indice en dehors des bornes!" << endl; cout << "Matrice: " << mat->nc << " colonnes, " << mat->nl << " lignes." << endl; cout << "Requete: ligne " << ligne << ", colonne " << col << endl; } } Matrice *alloue_matrice( int nl, int nc) { Matrice *res = new Matrice; res->nl = nl; res->nc = nc; res->tab = new float [nc * nl]; return res; } void libere_matrice(Matrice *mat) { delete [] mat->tab; delete mat; } void affiche(Matrice *mat) { for ( int i=0; i<mat->nl; i++) { for ( int j=0; j<mat->nc; j++) cout << get(mat, i, j) << " " ; cout << endl; } } bool set_id(Matrice *mat) { if (mat->nc == mat->nl) { for ( int i=0; i<mat->nl; i++) for ( int j=0; j<mat->nc; j++) if (i == j) set(mat, i, j, 1); else set(mat, i, j, 0); return true ; } else { cout << "Erreur [set_id]: matrice non carree." << endl; return false ; } } void set_0(Matrice *mat) { for ( int i=0; i<mat->nl; i++) for ( int j=0; j<mat->nc; j++) set(mat, i, j, 0); } // pour matrices a 6 coefficients void init(Matrice *mat, float c1, float c2, float c3, float c4, float c5, float c6) { if ((mat->nc == 3 && mat->nl == 2) || (mat->nc == 2 && mat->nl ==3)) { mat->tab[0] = c1; mat->tab[1] = c2; mat->tab[2] = c3; mat->tab[3] = c4; mat->tab[4] = c5; mat->tab[5] = c6; } else cout << "Erreur [init]: matrice de taille incorrecte." << endl; } // pour matrices a 9 coefficients void init(Matrice *mat, float c1, float c2, float c3, float c4, float c5, float c6, float c7, float c8, float c9) { if (mat->nc == 3 && mat->nl == 3) { mat->tab[0] = c1; mat->tab[1] = c2; mat->tab[2] = c3; mat->tab[3] = c4; mat->tab[4] = c5; mat->tab[5] = c6; mat->tab[6] = c7; mat->tab[7] = c8; mat->tab[8] = c9; } else cout << "Erreur [init]: matrice de taille incorrecte." << endl; } // pour matrices a 12 coefficients void init(Matrice *mat, float c1, float c2, float c3, float c4, float c5, float c6, float c7, float c8, float c9, float c10, float c11, float c12) { if ((mat->nc == 2 && mat->nl == 6) || (mat->nc == 3 && mat->nl == 4) || (mat->nc == 4 && mat->nl == 3) || (mat->nc == 6 && mat->nl == 2)) { mat->tab[0] = c1; mat->tab[1] = c2; mat->tab[2] = c3; mat->tab[3] = c4; mat->tab[4] = c5; mat->tab[5] = c6; mat->tab[6] = c7; mat->tab[7] = c8; mat->tab[8] = c9; mat->tab[9] = c10; mat->tab[10] = c11; mat->tab[11] = c12; } else cout << "Erreur [init]: matrice de taille incorrecte." << endl; } void copie(Matrice *mat, Matrice *res) { if (mat->nl == res->nl && mat->nc == res->nc) for ( int i=0; i<mat->nl; i++) for ( int j=0; j<mat->nc; j++) set(res, i, j, get(mat, i, j)); else cout << "Erreur [copie]: la taille des matrices ne correspond pas." << endl; } void add_mat(Matrice *m1, Matrice *m2, Matrice *res) { if (m1->nc == m2->nc && m1->nl == m2->nl && res->nc == m1->nc && res->nl == m1->nl) for ( int i=0; i<m1->nl; i++) for ( int j=0; j<m1->nc; j++) set(res, i, j, get(m1, i, j) + get(m2, i, j)); else cout << "Erreur [add_mat]: les matrices n'ont pas la meme taille." << endl; } float trace(Matrice *mat) { float tr = 0; if (mat->nl == mat->nc) for ( int i=0; i<mat->nl; i++) tr += get(mat, i, i); else cout << "Erreur [trace]: matrice non carree." << endl; return tr; } void mult(Matrice *m1, Matrice *m2, Matrice *res) { if (m1->nc == m2->nl && res->nl == m1->nl && res->nc == m2->nc) for ( int i=0; i<m1->nl; i++) for ( int j=0; j<m2->nc; j++) { res->tab[i * res->nc + j] = 0; for ( int k=0; k<m1->nc; k++) res->tab[i * res->nc + j] += get(m1, i, k) * get(m2, k, j); } else cout << "Erreur [mult_mat]: la taille des matrices ne correspondent pas." << endl; } void transpose_mat(Matrice *mat, Matrice *res) { if (mat->nc == res->nl && mat->nl == res->nc) for ( int i=0; i<res->nl; i++) for ( int j=0; j<res->nc; j++) set(res, i, j, get(mat, j, i)); else cout << "Erreur [transpose_mat]: la taille des matrices ne correspondent pas." << endl; } // ******* Fonctions pour Vecteur ******* float get(Vecteur *vec, int n) { if (n >= 0 && n < vec->n) return vec->tab[n]; else { cout << "Erreur [get]: indice en dehors des bornes!" << endl; cout << "Vecteur: " << vec->n << " elements" << endl; cout << "Requete: element " << n << endl; } return 0; } void set(Vecteur *vec, int n, float val) { if (n >= 0 && n < vec->n) vec->tab[n] = val; else { cout << "Erreur [set]: indice en dehors des bornes!" << endl; cout << "Vecteur: " << vec->n << " elements" << endl; cout << "Requete: element " << n << endl; } } Vecteur *alloue_vecteur( int n) { Vecteur *res = new Vecteur; res->n = n; res->tab = new float [n]; return res; } void libere_vecteur(Vecteur *vec) { delete [] vec->tab; delete vec; } void affiche(Vecteur *vec) { for ( int i=0; i<vec->n; i++) cout << get(vec, i) << endl; } void init(Vecteur *vec, float c1, float c2, float c3, float c4) { if (vec->n == 4) { vec->tab[0] = c1; vec->tab[1] = c2; vec->tab[2] = c3; vec->tab[3] = c4; } else cout << "Erreur [init]: vecteur de taille incorrecte." << endl; } float norm_vect(Vecteur *vec) { float somme = 0; for ( int i=0; i<vec->n; i++) somme += get(vec, i) * get(vec, i); return sqrt (somme); } float produit_scalaire(Vecteur *v1, Vecteur *v2) { float produit = 0; if (v1->n != v2->n) cout << "Erreur [produit_scalaire]: les vecteurs n'ont pas la meme taille." << endl; else { for ( int i=0; i<v1->n; i++) produit += v1->tab[i] * v2->tab[i]; } return produit; } |
- Saturday
- May 17th, 2025
- Ajouter un cours