-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.h
93 lines (77 loc) · 2.87 KB
/
Matrix.h
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
#include <assert.h> // Defines the assert function.
class Matrix {
public:
// Default Constructor. Creates a 1 by 1 matrix; sets value to zero.
Matrix () {
nRow_ = 1; nCol_ = 1;
data_ = new double [1]; // Allocate memory
set(0.0); // Set value of data_[0] to 0.0
}
// Regular Constructor. Creates an nR by nC matrix; sets values to zero.
// If number of columns is not specified, it is set to 1.
Matrix(int nR, int nC = 1) {
assert(nR > 0 && nC > 0); // Check that nC and nR both > 0.
nRow_ = nR; nCol_ = nC;
data_ = new double [nR*nC]; // Allocate memory
assert(data_ != 0); // Check that memory was allocated
set(0.0); // Set values of data_[] to 0.0
}
// Copy Constructor.
// Used when a copy of an object is produced
// (e.g., passing to a function by value)
Matrix(const Matrix& mat) {
this->copy(mat); // Call private copy function.
}
// Destructor. Called when a Matrix object goes out of scope.
~Matrix() {
delete [] data_; // Release allocated memory
}
// Assignment operator function.
// Overloads the equal sign operator to work with
// Matrix objects.
Matrix& operator=(const Matrix& mat) {
if( this == &mat ) return *this; // If two sides equal, do nothing.
delete [] data_; // Delete data on left hand side
this->copy(mat); // Copy right hand side to l.h.s.
return *this;
}
// Simple "get" functions. Return number of rows or columns.
int nRow() const { return nRow_; }
int nCol() const { return nCol_; }
// Parenthesis operator function.
// Allows access to values of Matrix via (i,j) pair.
// Example: a(1,1) = 2*b(2,3);
// If column is unspecified, take as 1.
double& operator() (int i, int j = 1) {
assert(i > 0 && i <= nRow_); // Bounds checking for rows
assert(j > 0 && j <= nCol_); // Bounds checking for columns
return data_[ nCol_*(i-1) + (j-1) ]; // Access appropriate value
}
// Parenthesis operator function (const version).
const double& operator() (int i, int j = 1) const{
assert(i > 0 && i <= nRow_); // Bounds checking for rows
assert(j > 0 && j <= nCol_); // Bounds checking for columns
return data_[ nCol_*(i-1) + (j-1) ]; // Access appropriate value
}
// Set function. Sets all elements of a matrix to a given value.
void set(double value) {
int i, iData = nRow_*nCol_;
for( i=0; i<iData; i++ )
data_[i] = value;
}
//*********************************************************************
private:
// Matrix data.
int nRow_, nCol_; // Number of rows, columns
double* data_; // Pointer used to allocate memory for data.
// Private copy function.
// Copies values from one Matrix object to another.
void copy(const Matrix& mat) {
nRow_ = mat.nRow_;
nCol_ = mat.nCol_;
int i, iData = nRow_*nCol_;
data_ = new double [iData];
for(i = 0; i<iData; i++ )
data_[i] = mat.data_[i];
}
}; // Class Matrix