- 论坛徽章:
- 0
|
回复 6# changsha
matrix.h
- #ifndef MATRIX_H
- #define MATRIX_H
- #include <iostream>
- using namespace std;
- template<typename T>
- class Matrix
- {
- private:
- class Matrix1D;
- public:
- int rows,cols;
- T *data;
- Matrix(int _rows, int _cols, T *data);
- ~Matrix();
- Matrix operator+(const Matrix &mat);
- Matrix operator*(const Matrix &mat);
- bool operator==(const Matrix &mat);
- Matrix1D operator[](int row){Matrix1D m1d(row, cols, data);return m1d;}
- operator T();
- };
- template<typename T>
- ostream& operator<<(ostream& os, const Matrix<T> &mat);
- template<typename T>
- istream& operator>>(istream& is, Matrix<T> &mat);
- template<typename T>
- class Matrix<T>::Matrix1D
- {
- private:
- int inrow, cols;
- T *indata;
- public:
- Matrix1D(int _row, int _cols, T *_data):inrow(_row), cols(_cols),indata(_data){}
- T operator[](int col){return indata[inrow*cols+col];}
- };
- template<typename T>
- class Vector3:public Matrix<T>
- {
- public:
- Vector3(T t1, T t2, T t3):Matrix<T>(3, 1, NULL){
- this->data[0]=t1;this->data[1]=t2;this->data[2]=t3;}
- T operator[](int index){return this->data[index];}
- };
- #endif
复制代码 matrix.cpp
- #include "matrix.h"
- #include <string.h>
- #include <fstream>
- #ifdef DEBUG
- #include <assert.h>
- #endif
- template<typename T>
- Matrix<T>::Matrix(int _rows, int _cols, T *_data)
- {
- rows=_rows,cols=_cols;
- data = new T[rows*cols];
- if (_data == NULL)
- {
- bzero(data, rows*cols);
- }
- else
- {
- for(int i=0; i < rows*cols; i++)
- {
- data[i] = _data[i];
- }
- }
- }
- template<typename T>
- Matrix<T>::~Matrix()
- {
- //std::cout << "~Matrix" << std::endl;
- if(data != NULL)
- {
- delete[] data;
- }
- }
- template<typename T>
- Matrix<T> Matrix<T>::operator+(const Matrix<T> &mat)
- {
- #ifdef DEBUG
- assert(this->rows==mat.rows && this->cols==mat.cols);
- #endif
- Matrix<T> result(rows, cols, NULL);
- for(int i=0; i < rows*cols; i++)
- {
- result.data[i] = this->data[i]+mat.data[i];
- }
- return result;
- }
- template<typename T>
- Matrix<T> Matrix<T>::operator*(const Matrix<T> &mat)
- {
- #ifdef DEBUG
- assert(cols==mat.rows);
- #endif
- Matrix<T> result(rows, mat.cols, NULL);
- for(int i=0; i < result.rows; i++)
- {
- for(int j=0; j < result.cols; j++)
- {
- for(int k=0; k < cols; k++)
- {
- result.data[i*result.cols+j] += this->data[i*cols+k]*mat.data[k*mat.cols+j];
- }
- }
- }
- return result;
- }
- template<typename T>
- ostream& operator<<(ostream& os, const Matrix<T> &mat)
- {
- #ifdef DEBUG
- assert(mat.rows!=0&&mat.cols!=0);
- #endif
- os << mat.rows << "\t" << mat.cols << endl;
- for(int i=0; i < mat.rows; i++)
- {
- for(int j=0; j < mat.cols; j++)
- {
- os << mat.data[i*mat.cols+j] << "\t";
- }
- os << endl;
- }
- return os;
- }
- template<typename T>
- istream& operator>>(istream& is, Matrix<T> &mat)
- {
- if(mat.data != NULL)
- delete[] mat.data;
- is >> mat.rows >> mat.cols;
- mat.data = new T[mat.rows*mat.cols];
- for(int i = 0; i < mat.rows; i++)
- {
- for(int j = 0; j < mat.cols; j++)
- {
- is >> mat.data[i*mat.cols+j];
- }
- }
- return is;
- }
- template<typename T>
- Matrix<T>::operator T()
- {
- #ifdef DEBUG
- assert(rows==1&&cols==1);
- #endif
- return data[0];
- }
- template<typename T>
- bool Matrix<T>::operator==(const Matrix &mat)
- {
- if(this->rows==mat.rows&&this->cols==mat.cols)
- {
- for(int i = 0; i < this->rows*this->cols; i++)
- {
- if(this->data[i] != mat.data[i])
- return false;
- }
- return true;
- }
- return false;
- }
复制代码 |
|