-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataType.h
138 lines (123 loc) · 4.42 KB
/
DataType.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
#ifndef IMAGE_MESSURE_DATATYPE_H
#define IMAGE_MESSURE_DATATYPE_H
#include <ostream>
#include <functional>
#include <vector>
#include "iomanip"
namespace Algebric{
template <typename T> class Matrix;
template <typename T> class MultiDimPoint {
private:
std::vector<T> mdp;
int dim;
public:
MultiDimPoint(const std::initializer_list<T> &arg);
MultiDimPoint(const int &dim);
MultiDimPoint();
MultiDimPoint(const std::vector<T>&);
MultiDimPoint& setValue(T p,const int& index);
T getValue(int index) const;
int getDim() const;
std::vector<T> getVec()const;
const T& operator()(const int &index)const{
return getValue(index);
};
T& operator()(const int &index){
return (mdp.at(index));
};
template<typename K> friend MultiDimPoint<T> operator*(const K& k,const MultiDimPoint<T> &o){
std::vector<T> t = o.getVec();
for(T &s:t)
s = k*s;
return t;
}
MultiDimPoint<T> operator+(const MultiDimPoint<T> &vec2)const{
if(getDim() == vec2.getDim())
{
std::vector<T> res;
for (int i = 0; i < getDim(); ++i) {
res.push_back(getValue(i)+vec2.getValue(i));
}
return res;
}
}
MultiDimPoint<T> operator-(const MultiDimPoint<T> &vec2)const {
return operator+(-1*vec2);
}
Matrix<T> operator^(const MultiDimPoint<T>& vec2)const;
T Norm()const;
friend std::ostream& operator<<(std::ostream& o ,const MultiDimPoint& mdPoint){
o<<"(";
for(int i=0 ; i<mdPoint.getDim();i++){
if(i!=mdPoint.getDim()-1){
o<<mdPoint.getValue(i) <<" ,";
} else{
o<<mdPoint.getValue(i)<<")";
}
}
return o;
};
};
template <typename T> class Matrix {
private:
std::vector<std::vector<T> > mat;
unsigned rows;
unsigned cols;
public:
Matrix(unsigned _rows, unsigned _cols, const T& _initial);
Matrix(const Matrix<T>& rhs);
virtual ~Matrix();
// Operator overloading, for "standard" mathematical matrix operations
Matrix<T>& operator=(const Matrix<T>& rhs);
// Matrix mathematical operations
Matrix<T> operator+(const Matrix<T>& rhs);
Matrix<T>& operator+=(const Matrix<T>& rhs);
Matrix<T> operator-(const Matrix<T>& rhs);
Matrix<T>& operator-=(const Matrix<T>& rhs);
Matrix<T> operator*(const Matrix<T>& rhs);
Matrix<T>& operator*=(const Matrix<T>& rhs);
Matrix<T> transpose();
// Matrix/scalar operations
Matrix<T> operator+(const T& rhs);
Matrix<T> operator-(const T& rhs);
Matrix<T> operator*(const T& rhs);
Matrix<T> operator/(const T& rhs);
// Matrix/vector operations
std::vector<T> operator*(const std::vector<T>& rhs);
MultiDimPoint<T> operator*(const MultiDimPoint<T>& rhs);
std::vector<T> diag_vec();
// Access the individual elements
T& operator()(const unsigned& row, const unsigned& col);
const T& operator()(const unsigned& row, const unsigned& col) const;
// Access the row and column sizes
unsigned get_rows() const;
unsigned get_cols() const;
void pretty_row(int which,std::ostream& stream)const{
using namespace std;
stream<<setprecision(3)<<"| ";
for (int i = 0; i < get_cols(); ++i) {
if(mat[which][i]>=0){
stream<<setw(6)<<left<<mat[which][i]<<" ";
if(i == get_cols() -1)
stream<<"\b\b\b";
}
else {
stream << '\b' << setw(6) << left << mat[which][i] << " ";
if(i == get_cols() -1)
stream<<"\b\b\b";
}
}
stream<<"|";
}
// better print!
friend std::ostream& operator<<(std::ostream& stream,const Matrix& m){
for(int i=0;i<m.get_rows();i++){
m.pretty_row(i,stream);
stream<<"\n";
}
return stream;
}
};
};
#include "DataType.cpp"
#endif //MATHUTILS_DATATYPE_H