-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMatrix.h
More file actions
120 lines (93 loc) · 2.27 KB
/
Copy pathMatrix.h
File metadata and controls
120 lines (93 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <cassert>
#ifndef Matrix_class
#define Matrix_class
template<typename T>
class Matrix {
public:
typedef T value_type;
//set all elements to zero
Matrix()
: nrow_(0), ncol_(0)
{}
//allocate number of row col and elements
Matrix(int nrow,int ncol)
: nrow_(nrow),ncol_(ncol),data_(nrow*ncol)
{}
// copy constructor
Matrix(const Matrix<T>& m) {
nrow_=m.nrow_;
ncol_=m.ncol_;
data_=m.data_;
}
const T& operator()(int i,int j) const;
T& operator()(int i, int j);
void print();
void resize(int newrow, int newcol);
int n_row();
int n_col();
void fill(T val);
void clear();
private:
int nrow_,ncol_;
std::vector<T> data_;
};
/*
* ***********
* Functions in Class Matrix ------
* ***********
*/
template<class T>
int Matrix<T>::n_row() {
return nrow_;
} // ----------
template<class T>
int Matrix<T>::n_col() {
return ncol_;
} // ----------
template<class T>
void Matrix<T>::fill(T val) {
std::fill(data_.begin(),data_.end(),val);
} // ----------
template<class T>
void Matrix<T>::resize(int newrow, int newcol) {
// assert(i<nrow_ && j<ncol_);
// assert(i+j*nrow_<data_.size());
//~ cout<<"newsize "<< newrow*newcol << endl;
nrow_=newrow;
ncol_=newcol;
data_.clear();
data_.resize(newrow*newcol);
} // ----------
template<class T>
void Matrix<T>::clear() {
// assert(i<nrow_ && j<ncol_);
// assert(i+j*nrow_<data_.size());
//~ cout<<"newsize "<< newrow*newcol << endl;
nrow_=0;
ncol_=0;
data_.clear();
} // ----------
template<class T>
const T& Matrix<T>::operator()(int i, int j) const{
// assert(i<nrow_ && j<ncol_);
// assert(i+j*nrow_<data_.size());
return data_[i+j*nrow_];
} // ----------
template<class T>
T& Matrix<T>::operator()(int i,int j){
// assert(i<nrow_ && j<ncol_);
// assert(i+j*nrow_<data_.size());
return data_[i+j*nrow_];
} // ----------
template<class T>
void Matrix<T>::print(){
std::cout<<"shape:= ("<<nrow_<<","<<ncol_<<")"<<std::endl;
for(int i=0; i<nrow_; i++) {
for(int j=0; j<ncol_; j++) {
std::cout << data_[i+j*nrow_] << "\t";
}
std::cout << std::endl;
}
return;
} // ----------
#endif