/usr/include/psurface/SparseMatrix.h is in libpsurface-dev 2.0.0-2+b1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 178 179 180 | #ifndef SPARSE_MATRIX_H
#define SPARSE_MATRIX_H
#include <vector>
#include "Vector.h"
#include "StaticVector.h"
#include <stdexcept>
#include <sstream>
namespace psurface {
/** A template class for sparse matrices.
*
* \tparam T one of float, double, complex<float> or complex<double>.
*/
template<class T> class SparseMatrix
{
protected:
struct MatrixEntry{
MatrixEntry() : value(T(0)), col(0){};
MatrixEntry(const T& newVal, int column){
value = newVal;
col = column;
}
T value;
int col;
};
///
std::vector<std::vector<MatrixEntry> > data;
///
int numCols;
public:
/// Default Constructor
SparseMatrix() : numCols(0) {
data.clear();
}
///
SparseMatrix(int n) : numCols(n) {
data.resize(n);
for (int i=0; i<n; i++){
data[i].resize(1);
data[i][0] = MatrixEntry(T(0), i);
}
}
/// Multiplication with a scalar
void operator*=(const T& scalar) {
for (size_t i=0; i<data.size(); i++)
for (size_t j=0; j<data[i].size(); j++)
data[i][j].value *= scalar;
}
/// The number of rows of the matrix
size_t nRows() const {
return data.size();
}
/// The number of columns of the matrix
size_t nCols() const {
return numCols;
}
///
void setEntry(int i, int j, const T& newValue) {
for (size_t k=0; k<data[i].size(); k++)
if (data[i][k].col==j){
data[i][k].value = newValue;
return;
}
data[i].push_back(MatrixEntry(newValue, j));
}
///
void addToEntry(int i, int j, const T& newValue) {
for (size_t k=0; k<data[i].size(); k++)
if (data[i][k].col==j){
data[i][k].value += newValue;
return;
}
data[i].push_back(MatrixEntry(newValue, j));
}
///
Vector<T> multVec(const Vector<T>& v) const {
assert(v.size()==nCols());
Vector<T> result(v.size(), StaticVector<T, 2>(0));
for (size_t i=0; i<nRows(); i++)
for (size_t j=0; j<data[i].size(); j++)
result[i] += data[i][j].value * v[data[i][j].col];
return result;
}
/// another iterative solver for nonsymmetric matrices: BI-CGSTAB
size_t BiCGSTAB(const Vector<T>& b, Vector<T>& x, Vector<T>& r,
const size_t& maxIter, const T& tolerance) const {
const float EPSILON=1e-20;
const size_t n = numCols;
T rho, rho_old, alpha, beta, omega, h, norm0;
Vector<T> r0(n), p(n, StaticVector<T, 2>(0)), v(n, StaticVector<T, 2>(0)), s(n), t(n);
r = r0 = b - multVec(x);
norm0 = r0.length();
rho_old = alpha = omega = 1;
if (r.length() < (tolerance * norm0) || r.length() < 1e-15)
return 0;
for (size_t k = 0; k < maxIter; ++k) {
rho = r0 * r;
if (std::abs(rho) <= EPSILON) {
std::ostringstream os;
os << "Breakdown in BiCGSTAB - rho "
<< rho << " <= EPSILON " << EPSILON
<< " after " << k << " iterations";
throw std::runtime_error(os.str());
}
if (std::abs(omega) <= EPSILON) {
std::ostringstream os;
os << "Breakdown in BiCGSTAB - omega "
<< omega << " <= EPSILON " << EPSILON
<< " after " << k << " iterations";
throw std::runtime_error(os.str());
}
beta = (rho/rho_old)*(alpha/omega);
p = r + beta*(p - omega * v);
v = multVec(p);
h = r0 * v;
if (std::abs(h) < EPSILON) {
std::ostringstream os;
os << "Breakdown in BiCGSTAB - h "
<< h << " < EPSILON " << EPSILON
<< " after " << k << " iterations";
throw std::runtime_error(os.str());
}
alpha = rho/h;
x += alpha * p;
s = r - alpha * v;
if (s.length() < (tolerance * norm0))
return k;
t = multVec(s);
omega = (t*s)/(t*t);
x += omega * s;
r = s - omega*t;
rho_old = rho;
if (r.length() < (tolerance * norm0) || r.length() < 1e-15)
return k;
}
throw std::runtime_error("BiCGSTAB did not converge.");
}
};
} // namespace psurface
#endif
|