Advertisement
Guest User

Matrix 2

a guest
Feb 21st, 2015
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.23 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include <cmath>
  3.  
  4. #include <iostream>
  5. #include <iomanip>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. template <int n>
  11. class Matrix
  12. {
  13. public:
  14.     Matrix(int val = 0)
  15.     {
  16.         for (int i = 0; i < n; i++)
  17.             for (int j = 0; j < n; j++)
  18.                 elements[i][j] = val;
  19.     }
  20.  
  21.     // конструктор копирования. Особый конструктор для случаем такого типа:
  22.     // Matrix a(5, 2);
  23.     // Matrix b(a); - будет вызван конструктор копирования
  24.     // Конструктор копирования всегда принимает константную ссылку на объект
  25.     Matrix(const Matrix & other)
  26.     {
  27.         for (int i = 0; i < n; i++)
  28.             for (int j = 0; j < n; j++)
  29.                 elements[i][j] = other.elements[i][j];
  30.     }
  31.      
  32.     int & at(int i, int j)
  33.     {
  34.         return elements[i][j];
  35.     }
  36.  
  37.     int at(int i, int j) const
  38.     {
  39.         return elements[i][j];
  40.     }
  41.  
  42.     // Оператор = возвращает ссылку на сам объект. Это нужно для обработки выражений вида
  43.     // a = b = c;
  44.     // Сначала будет обработан вызов b.operator=(c), который вернёт при этом новое значение b,
  45.     // Которое послужит аргументом в вызове a.operator=( b.operator=(c) );
  46.     Matrix & operator =(const Matrix & right)
  47.     {
  48.         for (int i = 0; i < n; i++)
  49.             for (int j = 0; j < n; j++)
  50.                 elements[i][j] = right.elements[i][j];
  51.         return *this;
  52.     }
  53.  
  54.     Matrix operator +(const Matrix & a) const
  55.     {
  56.         Matrix res;
  57.         for (int i = 0; i < n; i++)
  58.             for (int j = 0; j < n; j++)
  59.                 res.elements[i][j] = a.elements[i][j] + elements[i][j];
  60.         return res; // в этот момент будет использован оператор = для Matrix
  61.     }
  62.  
  63.     Matrix operator *(const Matrix & a) const
  64.     {
  65.         Matrix res;
  66.         for (int i = 0; i < n; i++) // строки
  67.             for (int j = 0; j < n; j++) //столбцы
  68.                 for (int k = 0; k < n; k++) //элементы
  69.                     res.elements[i][j] += elements[i][k] * a.elements[k][j];
  70.         return res; // в этот момент будет использован оператор = для Matrix
  71.     }
  72.  
  73.     vector<int> operator *(const vector<int> v) const
  74.     {
  75.         vector<int> res(n);
  76.         for (int i = 0; i < n; i++) // строки
  77.             for (int k = 0; k < n; k++) //элементы
  78.                 res[i] += elements[i][k] * v[k];
  79.         return res;
  80.     }
  81.  
  82.     class MatrixString
  83.     {
  84.     public:
  85.         MatrixString (int * p, int n) : p(p), n(n) {}
  86.  
  87.         int & operator [](int i)
  88.         {
  89.             if (i >= n)
  90.                 throw exception("Wrong index");
  91.             return p[i];
  92.         }
  93.  
  94.         int operator [](int i) const
  95.         {
  96.             if (i >= n)
  97.                 throw exception("Wrong index");
  98.             return p[i];
  99.         }
  100.     private:
  101.         int * p, n;
  102.     };
  103.  
  104.     MatrixString operator [](int i)
  105.     {
  106.         if (i >= n)
  107.             throw exception("Wrong index");
  108.         return MatrixString( (int*)elements + i*n, n );
  109.     }
  110.  
  111.     template <int n>
  112.     friend Matrix pow(const Matrix <n> & a, int p)
  113.     {
  114.         Matrix <n> res;
  115.         for (int i = 0; i < n; i++)
  116.             res.elements[i][i] = 1;
  117.  
  118.         Matrix current(a); // будет вызван конструктор копирования
  119.  
  120.         while (p)
  121.         {
  122.             if (p & 1)
  123.             {
  124.                 res = res * current;
  125.             }
  126.  
  127.             current = current * current;
  128.  
  129.             p >>= 1;
  130.         }
  131.         return res; // в этот момент будет использован оператор = для Matrix
  132.     }
  133.  
  134. private:
  135.     int elements[n][n];
  136. };
  137.  
  138. int main()
  139. {
  140.     Matrix<2> a;
  141.     try
  142.     {
  143.         a[0][2] = 1;
  144.         a[0][1] = 1;
  145.         a[1][0] = 1;
  146.     }
  147.     catch (const exception & e)
  148.     {
  149.         cout << e.what();
  150.         return 1;
  151.     }
  152.  
  153.     for (int i = 0; i <= 20; i++)
  154.     {
  155.         Matrix<2> b = pow(a, i);
  156.  
  157.         vector <int> fib(2, 1);
  158.         fib = b * fib;
  159.  
  160.         cout << "F[" << i << "] = " << fib[1] << endl;
  161.  
  162.         // в этот момент вызывается деструктор матрицы b
  163.     }
  164.  
  165.     return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement