#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;
}