-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.hpp~
85 lines (69 loc) · 1.64 KB
/
Matrix.hpp~
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
/* Matrix Operations library for C++
Requires Vector.hpp for matrix/vector multiplication*/
#ifndef MATRIX_HPP
#define MATRIX_HPP
#include "Vector.hpp"
class Matrix
{
private:
int rows;
int cols;
// Vector data_;
std::vector<std::vector<double> > m;
public:
//Constructor
Matrix(int rows_, int cols_);
//get row/col
int numRows() const ;
int numCols() const ;
//access indiidual matrix elements
const double& operator()(const int& row, const int& col) const;
double& operator()(const int& row,const int& col);
//matrix matrix operators
Matrix operator+(const Matrix& m_RHS);
Matrix operator-(const Matrix& m_RHS);
Matrix operator*(const Matrix& m_RHS);
//matrix/scalar
Matrix operator+(const double& a);
Matrix operator-(const double& a); // get rid of
Matrix operator*(const double& a);
Matrix operator/(const double& a);
//Matrix/vector operations
Vector operator*(const Vector& v);
// Gaussian elimination
Vector Solve();
// Vector Solve(const Vector& b); //instead of Gauss
//Matrix inverstion
// Matrix Invert();
};
inline std::ostream& operator<<(std::ostream& os, const Matrix& m_RHS)
{
os << "[";
for (int i=0; i<m_RHS.numRows(); i++)
{
for(int j=0; j<m_RHS.numCols(); j++)
{
if(j!=0) os << ", ";
os << m_RHS(i,j);
}
if(i!=m_RHS.numRows()-1) os << ";\n";
}
os << "]";
return os;
}
inline Matrix IMat(int rows, int cols)
{
if(rows!=cols)
{
throw std::domain_error("matrix must be square");
}
Matrix rtn(rows,cols);
int j = 0;
for(int i = 0; i<rows;i++)
{
rtn(i,j) = 1;
j++;
}
return rtn;
}
#endif