-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatview.h
102 lines (83 loc) · 3.19 KB
/
matview.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
#pragma once
#ifndef MATRIX_VIEW_H
#define MATRIX_VIEW_H
#include "matrix.h"
/*
Convention is to define a matrixView after the matrix has been defined.
If a matrix's scope is complete and matrixView of that matrix still
exists, it will likely invoke an error or invite unwanted behaviour
in the program.
Finding ways to implement the 'view of a matrix' concept while keeping
tab on the original matrix's scope.
*/
namespace linear{
template<typename DATA>
class matrixView {
private:
matrix<DATA>& orgMatrix;
range rowRange;
range colRange;
public:
matrixView(matrix<DATA>& mat) : orgMatrix(mat), rowRange(mat.rows()), colRange(mat.cols()) {}
matrixView(matrix<DATA>& mat, int startRow, int endRow, int startCol, int endCol):\
orgMatrix(mat), rowRange(startRow, endRow), colRange(startCol, endCol) {}
matrixView(matrix<DATA>& mat, range rowRng, range colRng) :\
orgMatrix(mat), rowRange(rowRng), colRange(colRng) {}
DATA& operator()(int r, int c) {
if (r < rowRange.start || r >= rowRange.end || c < colRange.start || c >= colRange.end) {
throw std::out_of_range("matrixView () - Index out of range.");
}
int adjustedRowIndex = r - rowRange.start;
int adjustedColIndex = c - colRange.start;
return orgMatrix(adjustedRowIndex, adjustedColIndex);
}
void display(const std::string msg=":-");
matrix<DATA> cvtToMatrix();
};
template<typename DATA>
matrix<DATA> matrixView<DATA>::cvtToMatrix() {
matrix<DATA> res = orgMatrix(rowRange,colRange);
return res;
}
template<typename DATA>
void matrixView<DATA>::display(const std::string msg) {
//experimental code
int i,j;
std::cout<<"\n[(matrixView)] "<<msg<<"\n";
// zero size matrix display
if(this->rowRange.size() == 0 || this->colRange.size() == 0) {
std::cout<<" (empty matrix)\n";
return;
}
int max_precision = MATRIX_PRECISION;
int padding = 1;
// Find the maximum number of digits in the matrix
int maxDigits = 1;
for (i = 0; i < this->rowRange.size(); ++i) {
for (j = 0; j < this->colRange.size(); ++j) {
std::stringstream stream;
stream << std::fixed << std::setprecision(max_precision) << orgMatrix(rowRange.start + i, colRange.start + j);
std::string str = stream.str();
size_t pos = str.find_last_not_of('0');
if (pos != std::string::npos && str[pos] == '.')
pos--;
maxDigits = std::max(maxDigits, static_cast<int>(pos + 1));
}
}
// Set the width based on the maximum number of digits
int width = maxDigits + padding;
for (i = 0; i < this->rowRange.size(); ++i) {
for (j = 0; j < this->colRange.size(); ++j) {
std::stringstream stream;
stream << std::fixed << std::setprecision(max_precision) << orgMatrix(rowRange.start + i, colRange.start + j);
std::string str = stream.str();
size_t pos = str.find_last_not_of('0');
if (pos != std::string::npos && str[pos] == '.')
pos--;
std::cout << std::setw(width) << str.substr(0, pos + 1);
}
std::cout << "\n";
}
}
}//linear namepsace
#endif