/usr/include/dune/localfunctions/utility/lfematrix.hh is in libdune-localfunctions-dev 2.2.1-2.
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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | #ifndef DUNE_ALGLIB_MATRIX_HH
#define DUNE_ALGLIB_MATRIX_HH
#include <cassert>
#include <vector>
#include "field.hh"
#if HAVE_ALGLIB
#include <alglib/amp.h>
#include <alglib/matinv.h>
#warning ALGLIB support is deprecated and will be dropped after DUNE 2.2 (cf. FS#931)
#endif
namespace Dune
{
template< class F, bool aligned = false >
class LFEMatrix;
template< class F, bool aligned >
class LFEMatrix
{
typedef LFEMatrix< F, aligned > This;
typedef std::vector< F > Row;
typedef std::vector<Row> RealMatrix;
public:
typedef F Field;
operator const RealMatrix & () const
{
return matrix_;
}
operator RealMatrix & ()
{
return matrix_;
}
template <class Vector>
void row( const unsigned int row, Vector &vec ) const
{
assert(row<rows());
for (int i=0;i<cols();++i)
field_cast(matrix_[row][i], vec[i]);
}
const Field &operator() ( const unsigned int row, const unsigned int col ) const
{
assert(row<rows());
assert(col<cols());
return matrix_[ row ][ col ];
}
Field &operator() ( const unsigned int row, const unsigned int col )
{
assert(row<rows());
assert(col<cols());
return matrix_[ row ][ col ];
}
unsigned int rows () const
{
return rows_;
}
unsigned int cols () const
{
return cols_;
}
const Field *rowPtr ( const unsigned int row ) const
{
assert(row<rows());
return &(matrix_[row][0]);
}
Field *rowPtr ( const unsigned int row )
{
assert(row<rows());
return &(matrix_[row][0]);
}
void resize ( const unsigned int rows, const unsigned int cols )
{
matrix_.resize(rows);
for (unsigned int i=0;i<rows;++i)
matrix_[i].resize(cols);
rows_ = rows;
cols_ = cols;
}
bool invert ()
{
assert( rows() == cols() );
std::vector<unsigned int> p(rows());
for (unsigned int j=0;j<rows();++j)
p[j] = j;
for (unsigned int j=0;j<rows();++j)
{
// pivot search
unsigned int r = j;
Field max = std::abs( (*this)(j,j) );
for (unsigned int i=j+1;i<rows();++i)
{
if ( std::abs( (*this)(i,j) ) > max )
{
max = std::abs( (*this)(i,j) );
r = i;
}
}
if (max == Zero<Field>())
return false;
// row swap
if (r > j)
{
for (unsigned int k=0;k<cols();++k)
std::swap( (*this)(j,k), (*this)(r,k) );
std::swap( p[j], p[r] );
}
// transformation
Field hr = Unity<Field>()/(*this)(j,j);
for (unsigned int i=0;i<rows();++i)
(*this)(i,j) *= hr;
(*this)(j,j) = hr;
for (unsigned int k=0;k<cols();++k)
{
if (k==j) continue;
for (unsigned int i=0;i<rows();++i)
{
if (i==j) continue;
(*this)(i,k) -= (*this)(i,j)*(*this)(j,k);
}
(*this)(j,k) *= -hr;
}
}
// column exchange
Row hv(rows());
for (unsigned int i=0;i<rows();++i)
{
for (unsigned int k=0;k<rows();++k)
hv[ p[k] ] = (*this)(i,k);
for (unsigned int k=0;k<rows();++k)
(*this)(i,k) = hv[k];
}
return true;
}
private:
RealMatrix matrix_;
unsigned int cols_,rows_;
};
#if HAVE_ALGLIB
template< unsigned int precision, bool aligned >
class LFEMatrix< amp::ampf< precision >, aligned >
{
public:
typedef amp::ampf< precision > Field;
private:
typedef LFEMatrix< amp::ampf< precision >, aligned > This;
typedef ap::template_2d_array< Field, aligned > RealMatrix;
public:
operator const RealMatrix & () const
{
return matrix_;
}
operator RealMatrix & ()
{
return matrix_;
}
template <class Vector>
void row( const unsigned int row, Vector &vec ) const
{
assert(row<rows());
for (unsigned int i=0;i<cols();++i)
field_cast(matrix_(row,i), vec[i]);
}
const Field &operator() ( const unsigned int row, const unsigned int col ) const
{
assert(row<rows());
assert(col<cols());
return matrix_( row, col );
}
Field &operator() ( const unsigned int row, const unsigned int col )
{
assert(row<rows());
assert(col<cols());
return matrix_( row, col );
}
unsigned int rows () const
{
return matrix_.gethighbound( 1 )+1;
}
unsigned int cols () const
{
return matrix_.gethighbound( 2 )+1;
}
const Field *rowPtr ( const unsigned int row ) const
{
assert(row<rows());
const int lastCol = matrix_.gethighbound( 2 );
ap::const_raw_vector< Field > rowVector = matrix_.getrow( row, 0, lastCol );
assert( (rowVector.GetStep() == 1) && (rowVector.GetLength() == lastCol+1) );
return rowVector.GetData();
}
Field *rowPtr ( const unsigned int row )
{
assert(row<rows());
const int lastCol = matrix_.gethighbound( 2 );
ap::raw_vector< Field > rowVector = matrix_.getrow( row, 0, lastCol );
assert( (rowVector.GetStep() == 1) && (rowVector.GetLength() == lastCol+1) );
return rowVector.GetData();
}
void resize ( const unsigned int rows, const unsigned int cols )
{
matrix_.setbounds( 0, rows-1, 0, cols-1 );
}
bool invert ()
{
assert( rows() == cols() );
int info;
matinv::matinvreport< precision > report;
matinv::rmatrixinverse< precision >( matrix_, rows(), info, report );
return (info >= 0);
}
private:
RealMatrix matrix_;
};
#endif
template< class Field, bool aligned >
inline std::ostream &operator<<(std::ostream &out, const LFEMatrix<Field,aligned> &mat)
{
for (unsigned int r=0;r<mat.rows();++r)
{
out << field_cast<double>(mat(r,0));
for (unsigned int c=1;c<mat.cols();++c)
{
out << " , " << field_cast<double>(mat(r,c));
}
out << std::endl;
}
return out;
}
}
#endif // #ifndef DUNE_ALGLIB_MATRIX_HH
|