-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.h
205 lines (155 loc) · 5.6 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//=============================================================================
// matrix.h
//
// A matrix of doubles
//
#ifndef _matrix_H
#define _matrix_H
//=============================================================================
class matrix {
public:
//---------------------------------------------------------------------------
// Constructors, destructor and assignment
//---------------------------------------------------------------------------
matrix(const int x, const int y);
// rectangular matrix constructor of x by y
// Precondition: x > 0, y > 0
matrix(const int n);
// constructs a nxn square matrix
// Precondition: n > 0
void operator=(const matrix& m);
void become(const matrix& m);
// assignment operators
// Precondition: m.valid()
// Postcondition: valid()
matrix(const matrix& m);
// copy constructor
// Precondition: m.valid()
~matrix();
// Destructor
//---------------------------------------------------------------------------
// programming methods
//---------------------------------------------------------------------------
const bool valid() const;
// returns m_valid
void validate();
// sets m_valid to true
void print(const bool precision = true) const;
// prints to standard output, if precision then it will try to work out how
// many characters to print
// Precondition: valid(), precision >= 0
//---------------------------------------------------------------------------
// member methods
//---------------------------------------------------------------------------
const double& operator()(const int i, const int j) const;
double& operator()(const int i, const int j);
const double& get(const int i, const int j) const;
double& get(const int i, const int j);
// returns item (i,j)
// Precondition: i < m_x, j < m_y, valid()
const double* column(const int i) const;
// returns the ith column
// Note: this gives ownership of the double* to the caller
// Precondition: valid(), i < m_x
const double* row(const int i) const;
// returns the ith row
// Note: this gives ownership of the double* to the caller
// Precondition: valid(), i < m_y
const int x() const;
// returns x
// Precondition: valid()
const int y() const;
// returns y
// Precondition: valid()
const double max() const;
// returns the maximum element
// Precondition: valid()
const double min() const;
// returns the minimum element
// Precondition: valid()
void change_size(const int x, const int y);
// grows or shrinks the matrix to x by y, retains data and fills with 0
// Precondition: valid(), x > 0, y > 0
//---------------------------------------------------------------------------
// matrix operatons
//---------------------------------------------------------------------------
matrix operator+(const matrix& m) const;
matrix operator+=(const matrix& m);
// matrix addition
// Precondition: valid(), m.valid(), m.x() == m_x, m.y() == m_y
matrix operator-(const matrix& m) const;
matrix operator-=(const matrix& m);
// matrix subtraction
// Precondition: valid(), m.valid(), m.x() == m_x, m.y() == m_y
matrix operator*(const matrix& m) const;
matrix operator*=(const matrix& m);
// matrix multiplication, returns a matrix of dimension m_y by m.x()
// Precondition: valid(), m.valid(), m_x = m.y()
matrix operator*(const double& d) const;
matrix operator*=(const double& d);
// scalar multiplication
// Precondition: valid(), m.valid()
const bool operator==(const matrix& m) const;
// equaility operator
// Precondition: valid(), m.valid(), m.x() == m_x, m.y() == m_y
const bool operator!=(const matrix& m) const;
// inequaility operator
// Precondition: valid(), m.valid(), m.x() == m_x, m.y() == m_y
void transpose();
// transposes the matrix
// Precondition: valid()
const double determinant() const;
// finds the determinant of the matrix
// Precondition: valid(), square()
matrix invert();
// inverts the matrix
// Precondition: vaild(), square()
void LUfactor(const matrix& lower, const matrix& upper);
// returns by reference a lower triangular matrix lower, and an upper
// triangular matrix upper such that this=lower*upper
// Will only give meaningful results if this(0,0) != 0 and will not work if
// determinant is 0 but checking this would take longer
// Precondition: valid(), square()
//---------------------------------------------------------------------------
// types of matrices
//---------------------------------------------------------------------------
void identity();
// sets to the identity matrix
// Precondition: valid(), square()
void scalar(const double& d);
// sets the matrix to the matrix dI, I is identity
// Precondition: valid(), square()
void zero();
// sets the matrix to zero
// Precondition: valid()
void constant(const double& d);
// sets the matrix to all elements d
// Precondition: valid()
const bool square() const;
// returns true if square
// Precondition: valid()
const bool triangular() const;
// returns true is the matrix is triangular
const bool lower_triangular() const;
// returns true is the matrix is lower triangular
const bool upper_triangular() const;
// returns true is the matrix is upper triangular
const bool diagonal() const;
// returns true is the matrix is diagonal
protected:
matrix();
// Prohibited default constructor
// functions
const double* array() const;
// returns the array
// Precondition: valid()
// variables
private:
// functions
// variables
double* m_array;
int m_x;
int m_y;
bool m_valid;
};
#endif