Advertisement
Tucancitto

Pb10. MatriceTemplate

May 26th, 2021 (edited)
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.32 KB | None | 0 0
  1. // Matrice.h
  2. #pragma once
  3. #include <iostream>
  4. #include <vector>
  5. #include <iomanip>
  6. using namespace std;
  7.  
  8. template<class T>
  9. class Matrice
  10. {
  11. private:
  12.     int ordin;
  13.     vector<vector<T>> elemente;
  14.  
  15.     Matrice<T> getMinor(int, int);
  16.  
  17.     void interschimbareLinii(int, int, int);
  18.     void multiplicareLinie(int, int, T);
  19.     void operatieLinii(int, int, int, T);
  20.     int liberColoana(int, int, int);
  21.  
  22. public:
  23.     Matrice() : Matrice(0) {}
  24.     Matrice(int);
  25.     Matrice(const Matrice<T>&);
  26.     ~Matrice();
  27.  
  28.     friend istream& operator >>(istream& in, Matrice<T>& M)
  29.     {
  30.         for (int i = 0; i < M.ordin; ++i)
  31.             for (int j = 0; j < M.ordin; ++j)
  32.                 in >> M[i][j];
  33.         return in;
  34.     }
  35.     friend ostream& operator <<(ostream& out, Matrice<T> M)
  36.     {
  37.         for (int i = 0; i < M.ordin; ++i, out << endl)
  38.             for (int j = 0; j < M.ordin; ++j)
  39.                 out << setw(10) << M[i][j] << ' ';
  40.         return out;
  41.     }
  42.  
  43.     vector<T>& operator[](int indexLinie)
  44.     {
  45.         return elemente[indexLinie];
  46.     }
  47.  
  48.     Matrice<T>& operator =(const Matrice<T>&);
  49.  
  50.     bool operator ==(const Matrice<T>& A)
  51.     {
  52.         if (ordin != A.ordin)
  53.             return false;
  54.         for (int i = 0; i < ordin; ++i)
  55.             for (int j = 0; j < ordin; ++j)
  56.                 if (elemente[i][j] != A.elemente[i][j])
  57.                     return false;
  58.         return true;
  59.     }
  60.     bool operator !=(const Matrice<T>& A)
  61.     {
  62.         if (ordin != A.ordin)
  63.             return true;
  64.         int nrElemEgale = 0;
  65.         for (int i = 0; i < ordin; ++i)
  66.             for (int j = 0; j < ordin; ++j)
  67.                 if (elemente[i][j] == A.elemente[i][j])
  68.                     nrElemEgale++;
  69.         if (nrElemEgale == ordin * ordin)
  70.             return false;
  71.  
  72.         return true;
  73.     }
  74.  
  75.     Matrice<T> operator +(Matrice<T>);
  76.     Matrice<T> operator -(Matrice<T>);
  77.     Matrice<T> operator *(Matrice<T>);
  78.     Matrice<T> operator *(T);
  79.  
  80.     Matrice<T>& operator += (const Matrice<T>&);
  81.     Matrice<T>& operator -= (const Matrice<T>&);
  82.     Matrice<T>& operator *= (const Matrice<T>&);
  83.  
  84.     Matrice<T> operator^(int);
  85.  
  86.     Matrice<T> getTranspusa();
  87.  
  88.     T operator !();
  89.     Matrice<T> getInversa();
  90. };
  91.  
  92. template<class T>
  93. Matrice<T>::Matrice(int ordin) : ordin(ordin)
  94. {
  95.     elemente.resize(ordin);
  96.     for (int i = 0; i < ordin; ++i)
  97.         for (int j = 0; j < ordin; ++j)
  98.             elemente[i].push_back(0);
  99. }
  100.  
  101. template<class T>
  102. Matrice<T>::Matrice(const Matrice<T>& M)
  103. {
  104.     ordin = M.ordin;
  105.     elemente.resize(ordin);
  106.  
  107.     for (int i = 0; i < ordin; ++i)
  108.         for (int j = 0; j < ordin; ++j)
  109.             elemente[i].push_back(M.elemente[i][j]);
  110. }
  111.  
  112. template<class T>
  113. Matrice<T>::~Matrice()
  114. {
  115.     elemente.clear();
  116.     elemente.shrink_to_fit();
  117. }
  118.  
  119. template<class T>
  120. Matrice<T>& Matrice<T>::operator=(const Matrice<T>& M)
  121. {
  122.     if (this != &M)
  123.     {
  124.         this->~Matrice();
  125.  
  126.         ordin = M.ordin;
  127.         elemente.resize(ordin);
  128.  
  129.         for (int i = 0; i < M.ordin; ++i)
  130.             for (int j = 0; j < M.ordin; ++j)
  131.                 elemente[i].push_back(M.elemente[i][j]);
  132.     }
  133.  
  134.     return *this;
  135. }
  136.  
  137. template<class T>
  138. Matrice<T> Matrice<T>::operator+(Matrice<T> M)
  139. {
  140.     Matrice<T> Suma(M.ordin);
  141.  
  142.     for (int i = 0; i < M.ordin; ++i)
  143.         for (int j = 0; j < M.ordin; ++j)
  144.             Suma[i][j] = elemente[i][j] + M[i][j];
  145.  
  146.     return Suma;
  147. }
  148.  
  149. template<class T>
  150. Matrice<T> Matrice<T>::operator-(Matrice<T> M)
  151. {
  152.     Matrice Diferenta(M.ordin);
  153.  
  154.     for (int i = 0; i < M.ordin; ++i)
  155.         for (int j = 0; j < M.ordin; ++j)
  156.             Diferenta[i][j] = elemente[i][j] - M[i][j];
  157.  
  158.     return Diferenta;
  159. }
  160.  
  161. template<class T>
  162. Matrice<T> Matrice<T>::operator*(Matrice<T> M)
  163. {
  164.     Matrice Produs(ordin);
  165.  
  166.     for (int i = 0; i < ordin; ++i)
  167.         for (int j = 0; j < M.ordin; ++j)
  168.             for (int k = 0; k < ordin; ++k)
  169.                 Produs[i][j] += elemente[i][k] * M[k][j];
  170.  
  171.     return Produs;
  172. }
  173.  
  174. template<class T>
  175. Matrice<T> Matrice<T>::operator*(T scalar)
  176. {
  177.     Matrice Produs(ordin);
  178.     for (int i = 0; i < ordin; ++i)
  179.         for (int j = 0; j < ordin; ++j)
  180.             Produs[i][j] = elemente[i][j] * scalar;
  181.  
  182.     return Produs;
  183. }
  184.  
  185. template<class T>
  186. Matrice<T>& Matrice<T>::operator+=(const Matrice<T>& M)
  187. {
  188.     *this = *this + M;
  189.     return *this;
  190. }
  191.  
  192. template<class T>
  193. Matrice<T>& Matrice<T>::operator-=(const Matrice<T>& M)
  194. {
  195.     *this = *this - M;
  196.     return *this;
  197. }
  198.  
  199. template<class T>
  200. Matrice<T>& Matrice<T>::operator*=(const Matrice<T>& M)
  201. {
  202.     *this = *this * M;
  203.     return *this;
  204. }
  205.  
  206. template<class T>
  207. Matrice<T> Matrice<T>::operator^(int putere)
  208. {
  209.     Matrice<T> matr(*this);
  210.     if (putere == 0)
  211.         return matr * matr.getInversa();
  212.  
  213.     for (int i = 1; i < putere; ++i)
  214.         matr *= *this;
  215.  
  216.     return matr;
  217. }
  218.  
  219. template<class T>
  220. Matrice<T> Matrice<T>::getTranspusa()
  221. {
  222.     Matrice<T> transp(*this);
  223.  
  224.     for (int i = 0; i < ordin; ++i)
  225.         for (int j = 0; j < ordin; ++j)
  226.             transp.elemente[i][j] = elemente[j][i];
  227.  
  228.     return transp;
  229. }
  230.  
  231. template<class T>
  232. Matrice<T> Matrice<T>::getMinor(int linie, int coloana)
  233. {
  234.     Matrice<T> minor(ordin - 1);
  235.  
  236.     int nr = 0;
  237.     for (int i = 0; i < ordin; ++i)
  238.         for (int j = 0; j < ordin; ++j)
  239.             if (i != linie && j != coloana)
  240.             {
  241.                 minor[nr / (ordin - 1)][nr % (ordin - 1)] = elemente[i][j];
  242.                 nr++;
  243.             }
  244.     return minor;
  245. }
  246.  
  247. template<class T>
  248. T Matrice<T>::operator!()
  249. {
  250.     if (ordin == 1)
  251.         return elemente[0][0];
  252.  
  253.     if (ordin == 2)
  254.         return elemente[0][0] * elemente[1][1] - elemente[0][1] * elemente[1][0];
  255.  
  256.     float suma = 0;
  257.     int semn = 1;
  258.     for (int j = 0; j < ordin; ++j)
  259.     {
  260.         Matrice<T> minor = this->getMinor(0, j);
  261.         suma += elemente[0][j] * (!minor) * semn;
  262.         semn = -semn;
  263.     }
  264.  
  265.     return suma;
  266. }
  267.  
  268. template<class T>
  269. void Matrice<T>::multiplicareLinie(int nrC, int linie, T a) // M_i(a) sau L_i -> a*L_i
  270. {
  271.     for (int i = 0; i < nrC; ++i)
  272.         elemente[linie][i] = elemente[linie][i] * a;
  273. }
  274.  
  275. template<class T>
  276. void Matrice<T>::interschimbareLinii(int nrC, int linie1, int linie2) //S_ij sau L_i <-> L_j
  277. {
  278.     for (int i = 0; i < nrC; ++i)
  279.         std::swap(elemente[linie1][i], elemente[linie2][i]);
  280. }
  281.  
  282. template<class T>
  283. void Matrice<T>::operatieLinii(int nrC, int linie1, int linie2, T a) //T_i(a*L_j) sau L_i -> a*L_j + L_i
  284. {
  285.     for (int j = 0; j < nrC; ++j)
  286.         if ((T)(elemente[linie1][j] - a * elemente[linie2][j]) < (T)0.000001 &&
  287.             (T)(elemente[linie1][j] - a * elemente[linie2][j]) > (T)-0.000001)
  288.             elemente[linie1][j] = 0;
  289.         else
  290.             elemente[linie1][j] = elemente[linie1][j] - a * elemente[linie2][j];
  291. }
  292.  
  293. template<class T>
  294. int Matrice<T>::liberColoana(int nrL, int linie, int coloana)
  295. {
  296.     for (int i = linie; i < nrL; ++i)
  297.         if (elemente[i][coloana] != 0)
  298.             return i;
  299.     return -1;
  300. }
  301.  
  302. template<class T>
  303. Matrice<T> Matrice<T>::getInversa() // Metoda Jordan-Gauss
  304. {
  305.     Matrice<T> mat(*this);
  306.     for (int i = 0; i < ordin; i++)
  307.         for (int j = 0; j < ordin; j++)
  308.             if (i == j)
  309.                 mat[i].push_back(1);
  310.             else
  311.                 mat[i].push_back(0);
  312.  
  313.     int contorLinie = 0, contorColoana = 0;
  314.     while (contorLinie < ordin && contorColoana < ordin)
  315.     {
  316.         if (mat[contorLinie][contorColoana] != 1 && mat[contorLinie][contorColoana] != 0)
  317.             mat.multiplicareLinie(2 * ordin, contorLinie, 1 / mat[contorLinie][contorColoana]);
  318.         else
  319.             if (mat[contorLinie][contorColoana] == 0)
  320.             {
  321.                 int indiceLinie = mat.liberColoana(ordin, contorLinie, contorColoana);
  322.                 if (indiceLinie != -1)
  323.                     mat.interschimbareLinii(2 * ordin, contorLinie, indiceLinie);
  324.                 else
  325.                     contorColoana++;
  326.             }
  327.             else
  328.                 if (mat[contorLinie][contorColoana] == 1)
  329.                 {
  330.                     for (int i = 0; i < ordin; i++)
  331.                         if (i != contorLinie && mat[i][contorColoana] != 0)
  332.                             mat.operatieLinii(2 * ordin, i, contorLinie, mat[i][contorColoana]);
  333.  
  334.                     contorLinie++;
  335.                     contorColoana++;
  336.                 }
  337.     }
  338.  
  339.     Matrice<T> inversa(ordin);
  340.  
  341.     for (int i = 0; i < ordin; ++i)
  342.         for (int j = ordin; j < 2 * ordin; ++j)
  343.         {
  344.             if (mat[i][j] == -0) mat[i][j] = 0;
  345.             inversa[i][j - ordin] = mat[i][j];
  346.         }
  347.  
  348.     return inversa;
  349. }
  350.  
  351. // Matrice.cpp
  352. #include "Matrice.h"
  353.  
  354. template<class T>
  355. Matrice<T> suma(Matrice<T> A, int ordin, int putere)
  356. {
  357.     Matrice<T> tA = A.getTranspusa(), Suma(ordin);
  358.     for (int i = 1; i <= putere; ++i)
  359.     {
  360.         Suma += (tA ^ i);
  361.  
  362.         cout << "(tA)^" << i;
  363.         if (i < putere)
  364.             cout << " + ";
  365.         else
  366.             cout << " = " << endl;
  367.     }
  368.  
  369.     return Suma;
  370. }
  371.  
  372. int main()
  373. {
  374.     int putere, ordin;
  375.     cout << "Introduceti ordinul matricei : ";
  376.     cin >> ordin;
  377.  
  378.     Matrice<float> A(ordin);
  379.     cout << "Introduceti matricea : \n";
  380.     cin >> A;
  381.     cout << "Introduceti puterea : ";
  382.     cin >> putere;
  383.  
  384.     cout << suma(A, ordin, putere) << endl;
  385.     cout << "Determinantul matricei A : " << (!A) << endl;
  386.     if (!A != 0)
  387.         cout << "Inversa matricei A : \n" << A.getInversa();
  388.     else
  389.         cout << "Matricea nu este inversabila. \n";
  390.  
  391.     return 0;
  392. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement