forked from sangmichaelxie/giraffe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_ops.h
167 lines (132 loc) · 3.94 KB
/
matrix_ops.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
/*
Copyright (C) 2015 Matthew Lai
Giraffe is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Giraffe is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MATRIX_OPS_H
#define MATRIX_OPS_H
#include <vector>
#include "Eigen/Dense"
#include "Eigen/Sparse"
typedef float FP;
typedef Eigen::Matrix<FP, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor> NNMatrix;
typedef Eigen::Matrix<FP, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> NNMatrixRM;
typedef Eigen::Matrix<FP, 1, Eigen::Dynamic, Eigen::RowMajor> NNVector;
using Eigen::MatrixBase;
struct MatrixRegion
{
int64_t i;
int64_t j;
int64_t rows;
int64_t cols;
};
template <typename T>
struct SemiSparseMatrix
{
int64_t rows;
int64_t cols;
struct SubMatrix
{
int64_t i;
int64_t j;
T m;
};
std::vector<SubMatrix> subMatrices;
};
template <typename T>
std::vector<MatrixRegion> MatrixToRegions(T toConvert) // matrix passed by value since we need a copy to modify anyways
{
std::vector<MatrixRegion> ret;
while (true)
{
MatrixRegion newRegion;
bool nonZeroFound = false;
// find the first nonzero
for (int64_t i = 0; i < toConvert.rows(); ++i)
{
for (int64_t j = 0; j < toConvert.cols(); ++j)
{
// we are looking for exact zeros, so we don't need to check with threshold
if (toConvert(i, j) != 0.0f)
{
newRegion.i = i;
newRegion.j = j;
nonZeroFound = true;
break;
}
}
if (nonZeroFound)
{
break;
}
}
if (!nonZeroFound)
{
// the matrix is all zero, so we are done!
break;
}
newRegion.rows = 0;
newRegion.cols = 0;
// try to grow in rows (only need to check 1 element at a time, since we are growing from a single element)
while ((newRegion.i + newRegion.rows) < toConvert.rows() && toConvert(newRegion.i + newRegion.rows, newRegion.j) != 0.0f)
{
++newRegion.rows;
}
// try to grow in cols (need to check 1 vector at a time)
while ((newRegion.j + newRegion.cols) < toConvert.cols() && toConvert.block(newRegion.i, newRegion.j + newRegion.cols, newRegion.rows, 1).all())
{
++newRegion.cols;
}
ret.push_back(newRegion);
assert(toConvert.block(newRegion.i, newRegion.j, newRegion.rows, newRegion.cols).all());
toConvert.block(newRegion.i, newRegion.j, newRegion.rows, newRegion.cols).setZero();
}
return ret;
}
template <typename T>
SemiSparseMatrix<T> ToSemiSparse(const T &m, const std::vector<MatrixRegion> &rois)
{
SemiSparseMatrix<T> ret;
ret.rows = m.rows();
ret.cols = m.cols();
for (const auto &roi : rois)
{
typename
SemiSparseMatrix<T>::SubMatrix subm;
subm.i = roi.i;
subm.j = roi.j;
subm.m = m.block(roi.i, roi.j, roi.rows, roi.cols);
ret.subMatrices.push_back(subm);
}
return ret;
}
template <typename EigenA, typename EigenB, typename EigenC>
void MultiplyWithSemiSparse(const EigenA &a, const SemiSparseMatrix<EigenB> &b, EigenC &c)
{
// c = a * b
c = EigenC::Zero(a.rows(), b.cols);
assert(a.rows() == 1);
for (const auto &subMatrix : b.subMatrices)
{
c.segment(subMatrix.j, subMatrix.m.cols()) += a.segment(subMatrix.i, subMatrix.m.rows()) * subMatrix.m;
}
}
template <typename EigenA, typename EigenB, typename EigenC>
void MatrixMultiplyWithSemiSparse(const EigenA &a, const SemiSparseMatrix<EigenB> &b, EigenC &c)
{
// c = a * b
c = EigenC::Zero(a.rows(), b.cols);
for (const auto &subMatrix : b.subMatrices)
{
c.block(0, subMatrix.j, c.rows(), subMatrix.m.cols()) += a.block(0, subMatrix.i, a.rows(), subMatrix.m.rows()) * subMatrix.m;
}
}
#endif // MATRIX_OPS_H