-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmatrixmisc.h
177 lines (164 loc) · 4.3 KB
/
matrixmisc.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
#ifndef MATRIXMISC_H
#define MATRIXMISC_H
#include <string>
#include <fstream>
#include "matrix.h"
namespace MatAlg
{
/*!
\brief function that summs every element of matrix.
*/
template<typename Type>
Type elementSum(const Matrix<Type> &filter)
{
double div = 0.0;
for(uint x = 0; x < filter.getRowSize(); x++)
{
for(uint y = 0; y < filter.getColumnSize(); y++)
{
div += filter[x][y];
}
}
return div;
}
/*!
\brief function that multiplies every element of
matrix with every element of another matrix.
*/
template<typename Type>
Type elementMul(const Matrix<Type> &first, const Matrix<Type> &second)
{
double result = 0.0;
uint columns = first.getColumnSize();
uint rows = first.getRowSize();
for(uint x = 0; x < rows; x++)
{
for(uint y = 0; y < columns; y++)
{
result += first[x][y]*second[x][y];
}
}
return result;
}
/*!
\brief save matrix to text file.
*/
template<typename Type>
void saveMatrix(const std::string &fileName, const Matrix<Type> &matrix)
{
std::ofstream textFile;
textFile.open(fileName);
textFile << matrix.getRowSize() << " ";
textFile << matrix.getColumnSize() << " ";
for(std::size_t row = 0; row < matrix.getRowSize(); row++)
{
for(std::size_t column = 0; column < matrix.getColumnSize(); column++)
{
textFile << matrix[row][column] << " ";
}
}
textFile.close();
}
/*!
\brief load matrix from text file.
*/
template<typename Type>
Matrix<Type> loadMatrix(const std::string &fileName)
{
std::ifstream textFile;
textFile.open(fileName);
std::size_t rows = 0;
std::size_t columns = 0;
textFile >> rows;
textFile >> columns;
if(textFile.fail())
throw std::logic_error("Error reading rows/columns.");
if(rows == 0 || columns == 0)
throw std::logic_error("Error reading matrix from file (0 rows/columns)");
Matrix<Type> matrix(rows, columns, Type(0.0));
for(std::size_t row = 0; row < rows; row++)
{
for(std::size_t column = 0; column < columns; column++)
{
if(textFile.eof())
throw std::logic_error("Unexpected end of matrix file.");
if(textFile.fail())
throw std::logic_error("Error reading matrix elements.");
textFile >> matrix[row][column];
}
}
textFile.close();
return matrix;
}
/*!
\brief convert matrix to human readable string.
*/
template <typename Type>
std::string toString(const Matrix<Type> &matrix)
{
std::string output = "";
for(std::size_t i = 0; i < matrix.getRowSize(); i++)
{
output += "[ ";
for(std::size_t j = 0; j < matrix.getColumnSize(); j++)
{
output += std::to_string(matrix[i][j]);
output += " ";
}
output += "]\n";
}
return output;
}
/*!
\brief create matrix with every element equal to 1.
*/
template <typename Type>
Matrix<Type> eye(const uint dimension)
{
Matrix<Type> eyeMatrix(dimension, dimension, Type(0.0));
for(std::size_t i = 0; i < dimension; i++)
{
eyeMatrix[i][i] = Type(1.0);
}
return eyeMatrix;
}
template <typename Type>
Type dotProduct(const Matrix<Type> &l, const Matrix<Type> &r)
{
Type product = Type(0.0);
if(l.getColumnSize() == r.getColumnSize() && l.getColumnSize() > l.getRowSize())
{
for(std::size_t column = 0; column < l.getColumnSize(); column++)
{
product += l[0][column]*r[0][column];
}
return product;
}
else if(l.getRowSize() == r.getRowSize() && l.getRowSize() > l.getColumnSize())
{
for(std::size_t row = 0; row < l.getRowSize(); row++)
{
product += l[row][0]*r[row][0];
}
return product;
}
else
{
throw std::logic_error("Vectors should be same.");
}
}
template <typename Type>
Matrix<Type> transpose(const Matrix<Type> &matrix)
{
Matrix<Type> result(matrix.getColumnSize(), matrix.getRowSize(), Type(0.0));
for(std::size_t row = 0; row < matrix.getRowSize(); row++)
{
for(std::size_t column = 0; column < matrix.getColumnSize(); column++)
{
result[column][row] = matrix[row][column];
}
}
return result;
}
}
#endif // MATRIXMISC_H