Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <fstream>
- using namespace std;
- template<typename T> class matrix{
- private:
- vector< vector<T> > matr;
- int rows, cols, position;
- public:
- matrix(); // пустой
- matrix(int _rows, int _cols); // с указанными размерами
- matrix(const matrix <T> &arg); // копирующий
- ~matrix();
- matrix operator = (const matrix <T> &arg); // без избыточных объектов
- bool operator == (const matrix <T> &arg);
- matrix operator + (matrix <T> &arg); // обычное сложение
- matrix operator * (const matrix <T> &arg); // умножение на матрицу
- matrix operator * (const T &single); // умножение на скаляр
- matrix operator ^ (int single); // возведение в степень
- matrix& operator << (const T &single); // поместить скаляр в матрицу как в поток
- void toFile(const char* FileName); // sta
- void fromFile(const char* FileName); // lda
- void toHtmlFile(const char* FileName);
- void toTexFile(const char* FileName); //сохранить в tex-файл
- matrix circuit (); // замыкание
- };
- template <typename T> matrix<T>::matrix(){
- rows=0;
- cols=0;
- position=0;
- }
- template <typename T> matrix<T>::matrix(int _rows, int _cols){
- rows = _rows;
- cols = _cols;
- position = 0;
- matr.resize(rows);
- for (int i=0; i<rows; i++)
- matr[i].resize(cols);
- }
- template <typename T> matrix<T>::matrix(const matrix <T> &arg){
- rows = arg.rows;
- cols = arg.cols;
- position = 0;
- matr.resize(rows);
- for (int i=0; i<rows; i++){
- matr[i].resize(cols);
- matr[i]=arg.matr[i];
- }
- }
- template <typename T> matrix<T>::~matrix(){}
- template <typename T> matrix<T> matrix<T>::operator = (const matrix <T> &arg){
- if (this==&arg)
- return *this;
- rows=arg.rows;
- cols=arg.cols;
- matr.resize(rows);
- for (int i=0; i<rows; i++){
- matr[i].resize(cols);
- for (int j=0; j<cols; j++)
- matr[i][j]=arg.matr[i][j];
- }
- return *this;
- }
- template <typename T> bool matrix<T>::operator == (const matrix <T> &arg){
- for (int i=0; i<rows; i++)
- for (int j=0; j<cols; j++)
- if (matr[i][j] != arg.matr[i][j])
- return false;
- return true;
- }
- template <typename T> matrix<T> matrix<T>::operator + (matrix <T> &arg){
- if (rows!=arg.rows || cols!=arg.cols)
- throw "Bad size";
- matrix <T> res(rows, cols);
- for (int i=0; i<rows; i++)
- for(int j=0; j<cols; j++)
- res.matr[i][j]=arg.matr[i][j]+matr[i][j];
- return res;
- }
- template <typename T> matrix<T> matrix<T>::operator * (const matrix <T> &arg){
- if (rows!=arg.cols)
- throw "Bad size";
- matrix <T> res(rows, arg.cols);
- for (int i=0; i<rows; i++)
- for(int j=0; j<arg.cols; j++){
- T temp(0);
- for (int k=0; k<cols; k++)
- temp=temp+matr[i][k]*arg.matr[k][j];
- res.matr[i][j]=temp;
- }
- return res;
- }
- template <typename T> matrix<T> matrix<T>::operator * (const T &single){
- matrix <T> res(rows, cols);
- for (int i=0; i<rows; i++)
- for (int j=0; j<cols; j++)
- res.matr[i][j]=matr[i][j]*single;
- return res;
- }
- template <typename T> matrix<T> matrix<T>::operator ^ (int times){
- if(rows!=cols)
- throw "Matrix must be square";
- matrix <T> res(rows, cols);
- for(int i=0; i<times; i++)
- res=res*(*this);
- return res;
- }
- template <typename T> matrix <T>& matrix<T>::operator << (const T &single){
- if(position <= cols*rows){
- matr[position/cols][position%cols]=single;
- position++;
- } else {
- position=0;
- matr[0][0]=single;
- }
- return *this;
- }
- template <typename T> void matrix<T>::fromFile(const char* FileName){
- ifstream in(FileName);
- for(int i=0; i<rows; i++)
- for(int j=0; j<cols; j++)
- in>>matr[i][j];
- in.close();
- }
- template <typename T> void matrix<T>::toFile(const char* FileName){
- ofstream out(FileName);
- for(int i=0; i<rows; i++){
- for(int j=0; j<cols; j++)
- out<<matr[i][j]<<"\t";
- out<<"\n";
- }
- out.close();
- }
- template <typename T> void matrix<T>::toHtmlFile(const char* FileName){
- ofstream out(FileName);
- out << "<!DOCTYPE html><html><head><title>Matrix "<<rows<<"x"<<cols<<"</title></head><body><table>";
- for(int i=0; i<rows; i++){
- out << "<tr>";
- for(int j=0; j<cols; j++)
- out<<"<td>"<<matr[i][j]<<"</td>";
- }
- out<<"</table></body></html>";
- out.close();
- }
- template <typename T> void matrix<T>::toTexFile(const char* FileName){
- ofstream out(FileName);
- out << "\\documentclass[12pt]{article}\n\\begin{document}\n\\begin{tabular}{";
- for(int i=0; i<cols; i++)
- out<<"c"; // выравнивание по центру
- out<<"}\n";
- for(int i=0; i<rows; i++){
- for(int j=0; j<cols-1; j++)
- out<<" "<<matr[i][j]<<" & ";
- out<<matr[i][cols-1]<<" \\\\\n";
- }
- out<<"\\end{tabular}\\end{document}";
- out.close();
- }
- template <typename T> matrix<T> matrix<T>::circuit(){
- if(rows!=cols)
- throw "Matrix must be square";
- matrix <T> res(rows, cols);
- for(int i=0; i<=rows; i++)
- res=res+(*this)^i;
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment