unvisibleman

Template matrix class on C++

Mar 3rd, 2013
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. template<typename T> class matrix{
  8. private:
  9.     vector< vector<T> > matr;
  10.     int rows, cols, position;
  11. public:
  12.     matrix(); // пустой
  13.     matrix(int _rows, int _cols); // с указанными размерами
  14.     matrix(const matrix <T> &arg); // копирующий
  15.     ~matrix();
  16.     matrix operator = (const matrix <T> &arg); // без избыточных объектов
  17.     bool operator == (const matrix <T> &arg);
  18.     matrix operator + (matrix <T> &arg); // обычное сложение
  19.     matrix operator * (const matrix <T> &arg); // умножение на матрицу
  20.     matrix operator * (const T &single); // умножение на скаляр
  21.     matrix operator ^ (int single); // возведение в степень
  22.     matrix& operator << (const T &single); // поместить скаляр в матрицу как в поток
  23.     void toFile(const char* FileName); // sta
  24.     void fromFile(const char* FileName); // lda
  25.     void toHtmlFile(const char* FileName);
  26.     void toTexFile(const char* FileName); //сохранить в tex-файл
  27.     matrix circuit (); // замыкание   
  28. };
  29.  
  30. template <typename T> matrix<T>::matrix(){
  31.     rows=0;
  32.     cols=0;
  33.     position=0;
  34. }
  35.  
  36. template <typename T> matrix<T>::matrix(int _rows, int _cols){
  37.     rows = _rows;
  38.     cols = _cols;
  39.     position = 0;
  40.     matr.resize(rows);
  41.     for (int i=0; i<rows; i++)
  42.         matr[i].resize(cols);
  43. }
  44.  
  45. template <typename T> matrix<T>::matrix(const matrix <T> &arg){
  46.     rows = arg.rows;
  47.     cols = arg.cols;
  48.     position = 0;
  49.     matr.resize(rows);
  50.     for (int i=0; i<rows; i++){
  51.         matr[i].resize(cols);
  52.         matr[i]=arg.matr[i];
  53.     }
  54. }
  55.  
  56. template <typename T> matrix<T>::~matrix(){}
  57.  
  58. template <typename T> matrix<T> matrix<T>::operator = (const matrix <T> &arg){
  59.     if (this==&arg)
  60.         return *this;
  61.     rows=arg.rows;
  62.     cols=arg.cols;
  63.     matr.resize(rows);
  64.     for (int i=0; i<rows; i++){
  65.         matr[i].resize(cols);
  66.         for (int j=0; j<cols; j++)
  67.             matr[i][j]=arg.matr[i][j];
  68.     }
  69.     return *this;
  70. }
  71.  
  72. template <typename T> bool matrix<T>::operator == (const matrix <T> &arg){
  73.     for (int i=0; i<rows; i++)
  74.         for (int j=0; j<cols; j++)
  75.             if (matr[i][j] != arg.matr[i][j])
  76.                 return false;
  77.     return true;
  78. }
  79.  
  80. template <typename T> matrix<T> matrix<T>::operator + (matrix <T> &arg){
  81.     if (rows!=arg.rows || cols!=arg.cols)
  82.         throw "Bad size";
  83.     matrix <T> res(rows, cols);
  84.     for (int i=0; i<rows; i++)
  85.         for(int j=0; j<cols; j++)
  86.             res.matr[i][j]=arg.matr[i][j]+matr[i][j];
  87.     return res;
  88. }
  89.  
  90. template <typename T> matrix<T> matrix<T>::operator * (const matrix <T> &arg){
  91.     if (rows!=arg.cols)
  92.         throw "Bad size";
  93.     matrix <T> res(rows, arg.cols);
  94.     for (int i=0; i<rows; i++)
  95.         for(int j=0; j<arg.cols; j++){
  96.             T temp(0);
  97.             for (int k=0; k<cols; k++)
  98.                 temp=temp+matr[i][k]*arg.matr[k][j];
  99.             res.matr[i][j]=temp;
  100.         }
  101.     return res;
  102. }
  103.  
  104. template <typename T> matrix<T> matrix<T>::operator * (const T &single){
  105.     matrix <T> res(rows, cols);
  106.     for (int i=0; i<rows; i++)
  107.         for (int j=0; j<cols; j++)
  108.             res.matr[i][j]=matr[i][j]*single;
  109.     return res;
  110. }
  111.  
  112. template <typename T> matrix<T> matrix<T>::operator ^ (int times){
  113.     if(rows!=cols)
  114.         throw "Matrix must be square";
  115.     matrix <T> res(rows, cols);
  116.     for(int i=0; i<times; i++)
  117.         res=res*(*this);
  118.     return res;
  119. }
  120.  
  121. template <typename T> matrix <T>& matrix<T>::operator << (const T &single){
  122.     if(position <= cols*rows){
  123.          matr[position/cols][position%cols]=single;
  124.          position++;
  125.     } else {
  126.         position=0;
  127.         matr[0][0]=single;
  128.     }
  129.     return *this;
  130. }
  131.  
  132. template <typename T> void matrix<T>::fromFile(const char* FileName){
  133.     ifstream in(FileName);
  134.     for(int i=0; i<rows; i++)
  135.         for(int j=0; j<cols; j++)
  136.             in>>matr[i][j];
  137.     in.close();
  138. }
  139.  
  140. template <typename T> void matrix<T>::toFile(const char* FileName){
  141.     ofstream out(FileName);
  142.     for(int i=0; i<rows; i++){
  143.         for(int j=0; j<cols; j++)
  144.             out<<matr[i][j]<<"\t";
  145.         out<<"\n";
  146.     }
  147.     out.close();
  148. }
  149.  
  150. template <typename T> void matrix<T>::toHtmlFile(const char* FileName){
  151.     ofstream out(FileName);
  152.     out << "<!DOCTYPE html><html><head><title>Matrix "<<rows<<"x"<<cols<<"</title></head><body><table>";
  153.     for(int i=0; i<rows; i++){
  154.         out << "<tr>";
  155.         for(int j=0; j<cols; j++)
  156.             out<<"<td>"<<matr[i][j]<<"</td>";
  157.     }
  158.     out<<"</table></body></html>";
  159.     out.close();
  160. }
  161.  
  162. template <typename T> void matrix<T>::toTexFile(const char* FileName){
  163.     ofstream out(FileName);
  164.     out << "\\documentclass[12pt]{article}\n\\begin{document}\n\\begin{tabular}{";
  165.     for(int i=0; i<cols; i++)
  166.         out<<"c"; // выравнивание по центру
  167.     out<<"}\n";
  168.     for(int i=0; i<rows; i++){
  169.         for(int j=0; j<cols-1; j++)
  170.             out<<" "<<matr[i][j]<<" & ";
  171.         out<<matr[i][cols-1]<<" \\\\\n";
  172.     }
  173.     out<<"\\end{tabular}\\end{document}";
  174.     out.close();
  175. }
  176.  
  177. template <typename T> matrix<T> matrix<T>::circuit(){
  178.     if(rows!=cols)
  179.         throw "Matrix must be square";
  180.     matrix <T> res(rows, cols);
  181.     for(int i=0; i<=rows; i++)
  182.         res=res+(*this)^i;
  183.     return res;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment