1#ifndef _DG_OPERATORS_DYN_
2#define _DG_OPERATORS_DYN_
50 SquareMatrix(
const unsigned n,
const T& value): n_(
n), data_(n_*n_, value) {}
58 template<
class InputIterator>
59 SquareMatrix( InputIterator first, InputIterator last, std::enable_if_t<!std::is_integral<InputIterator>::value>* = 0): data_(first, last)
61 unsigned n = std::distance( first, last);
62 n_ = (unsigned)sqrt( (
double)
n);
72 unsigned n = src.size();
73 n_ = (unsigned)sqrt( (
double)
n);
74 if( n_*n_!=
n)
throw Error(
Message(
_ping_)<<
"Wrong number of elements "<<
n<<
" need "<<n_*n_<<
"\n");
81 for(
unsigned i=0; i<n_*n_; i++)
93 return data_[ i*n_+j];
102 return data_[ i*n_+j];
110 unsigned size()
const {
return n_;}
119 data_.resize(
m*
m, val);
127 const std::vector<value_type>&
data()
const {
return data_;}
134 std::vector<value_type>&
data() {
return data_;}
144 if(!( i< n_ && k<n_))
throw Error(
Message(
_ping_) <<
"Out of range "<<i<<
" "<<k<<
" range is "<<n_<<
"\n");
145 for(
size_t j = 0; j<n_; j++)
147 std::swap( data_[i*n_+j], data_[k*n_+j]);
159 for(
unsigned i=0; i<n_; i++)
160 for(
unsigned j=0; j<i; j++)
162 std::swap( o.data_[i*n_+j], o.data_[j*n_+i]);
174 template<
class ContainerType1,
class ContainerType2>
175 void symv(
const ContainerType1& x, ContainerType2&
y)
const
177 for(
unsigned j=0; j<n_; j++)
180 for(
unsigned k=0; k<n_; k++)
181 y[j] += data_[j*n_+k]*
x[k];
193 template<
class value_type1,
class ContainerType1,
class value_type2,
class ContainerType2>
194 void symv( value_type1 alpha,
const ContainerType1& x, value_type2 beta, ContainerType2&
y)
const
196 for(
unsigned j=0; j<n_; j++)
199 for(
unsigned k=0; k<n_; k++)
200 y[j] +=
alpha*data_[j*n_+k]*
x[k];
210 for(
size_t i = 0; i < n_*n_; i++)
211 if( data_[i] !=
rhs.data_[i])
231 for(
unsigned i=0; i<n_*n_; i++)
232 temp.data_[i] = -data_[i];
244 for(
unsigned i=0; i<n_*n_; i++)
245 data_[i] += op.data_[i];
257 for(
unsigned i=0; i<n_*n_; i++)
258 data_[i] -= op.data_[i];
270 for(
unsigned i=0; i<n_*n_; i++)
340 unsigned n_ =
lhs.n_;
342 for(
unsigned i=0; i< n_; i++)
343 for(
unsigned j=0; j<n_; j++)
345 temp.data_[i*n_+j] = (T)0;
346 for(
unsigned k=0; k<n_; k++)
347 temp.data_[i*n_+j] +=
lhs.data_[i*n_+k]*
rhs.data_[k*n_+j];
361 template<
class ContainerType>
364 ContainerType out(
x);
378 unsigned n_ = mat.n_;
379 for(
size_t i=0; i < n_ ; i++)
381 for(
size_t j = 0;j < n_; j++)
382 os << mat(i,j) <<
" ";
397 template<
class Istream>
399 unsigned n_ = mat.n_;
400 for(
size_t i=0; i<n_; i++)
401 for(
size_t j=0; j<n_; j++)
408 std::vector<T> data_;
443 T pivot, determinant=(T)1;
444 unsigned pivotzeile, numberOfSwaps=0;
445 const size_t n =
m.size();
447 for(
size_t j = 0; j <
n; j++)
450 for(
size_t i = 0; i< j; i++)
452 thrust::host_vector<T> mik(i), mkj(i);
453 for(
size_t k=0; k<i; k++)
454 mik[k] =
m(i,k), mkj[k] =
m(k,j);
458 for(
size_t i = j; i<
n; i++)
460 thrust::host_vector<T> mik(j), mkj(j);
461 for(
size_t k=0; k<j; k++)
462 mik[k] =
m(i,k), mkj[k] =
m(k,j);
468 for(
size_t i = j+1; i <
n; i++)
469 if( fabs(
m(i,j)) > fabs(pivot))
471 pivot =
m(i,j), pivotzeile = i;
474 if( fabs(pivot) > 1e-15 )
478 m.swap_lines( pivotzeile, j);
483 for(
size_t i=j+1; i<
n; i++)
489 throw std::runtime_error(
"Matrix is singular!!");
491 if( numberOfSwaps % 2 != 0)
516 const unsigned n = in.
size();
517 std::vector<unsigned> pivot(
n);
519 T determinant =
lu_pivot( lu, pivot);
520 if( fabs(determinant ) == 0)
521 throw std::runtime_error(
"Determinant zero!");
522 for(
unsigned i=0; i<
n; i++)
524 std::vector<T> unit(
n, 0);
527 for(
unsigned j=0; j<
n; j++)
542template<
class real_type>
546 for(
unsigned i=0; i<
n; i++)
556 assert(p.size() == lu.
size() && p.size() == b.size());
557 const size_t n = p.size();
559 for(
size_t i = 0; i<
n; i++)
562 std::swap( b[ p[i] ], b[i]);
563 thrust::host_vector<T> lui(i), bi(i);
564 for(
size_t j = 0; j < i; j++)
565 lui[j] = lu(i,j), bi[j] = b[j];
569 for(
int i = n-1; i>=0; i--)
571 thrust::host_vector<T> lui(n-(i+1)), bi(n-(i+1));
572 for(
size_t j = i+1; j <
n; j++)
573 lui[j-(i+1)] = lu(i,j), bi[j-(i+1)] = b[j];
581template<
class real_type>
582SquareMatrix<real_type> pipj(
unsigned n)
584 SquareMatrix<real_type> op(n, 0);
585 for(
unsigned i=0; i<
n; i++)
586 op( i,i) = 2./(real_type)(2*i+1);
590template<
class real_type>
591SquareMatrix<real_type> pipj_inv(
unsigned n)
593 SquareMatrix<real_type> op(n, 0);
594 for(
unsigned i=0; i<
n; i++)
595 op( i,i) = (real_type)(2*i+1)/2.;
599template<
class real_type>
600SquareMatrix<real_type> pidxpj(
unsigned n)
602 SquareMatrix<real_type> op(n, 0);
603 for(
unsigned i=0; i<
n; i++)
604 for(
unsigned j=0; j<
n; j++)
615template<
class real_type>
616SquareMatrix<real_type> rirj(
unsigned n)
618 return SquareMatrix<real_type>( n, 1.);
621template<
class real_type>
622SquareMatrix<real_type> rilj(
unsigned n)
624 SquareMatrix<real_type> op( n, -1.);
625 for(
unsigned i=0; i<
n; i++)
626 for(
unsigned j=0; j<
n; j++)
632template<
class real_type>
633SquareMatrix<real_type> lirj(
unsigned n) {
634 return rilj<real_type>( n).transpose();
637template<
class real_type>
638SquareMatrix<real_type> lilj(
unsigned n)
640 SquareMatrix<real_type> op( n, -1.);
641 for(
unsigned i=0; i<
n; i++)
642 for(
unsigned j=0; j<
n; j++)
649template<
class real_type>
650SquareMatrix<real_type> ninj(
unsigned n)
652 SquareMatrix<real_type> op( n, 0.);
653 for(
int i=0; i<(int)
n; i++)
654 for(
int j=0; j<(int)
n; j++)
657 op( i,j) = 2./(2*i+1)/(2*j+1);
659 op( i,j) = -2./(2*i+1)/(2*j+1);
683 dg::create::lu_solve( lu, p, b);
class intended for the use in throw statements
Definition exceptions.h:83
small class holding a stringstream
Definition exceptions.h:29
A square nxn matrix.
Definition operator.h:31
SquareMatrix & operator-=(const SquareMatrix &op)
subtract
Definition operator.h:255
unsigned size() const
Size n of the SquareMatrix.
Definition operator.h:110
SquareMatrix & operator*=(const T &value)
scalar multiply
Definition operator.h:268
const std::vector< value_type > & data() const
access underlying data
Definition operator.h:127
SquareMatrix(const unsigned n)
allocate storage for nxn matrix
Definition operator.h:43
SquareMatrix(const unsigned n, const T &value)
Initialize elements.
Definition operator.h:50
void symv(value_type1 alpha, const ContainerType1 &x, value_type2 beta, ContainerType2 &y) const
Matrix vector multiplication .
Definition operator.h:194
const T & operator()(const size_t i, const size_t j) const
const access operator
Definition operator.h:101
void resize(unsigned m, T val=T())
Resize.
Definition operator.h:117
friend SquareMatrix operator*(const SquareMatrix &lhs, const T &value)
scalar multiplication
Definition operator.h:325
friend SquareMatrix operator*(const SquareMatrix &lhs, const SquareMatrix &rhs)
matrix multiplication
Definition operator.h:338
friend std::ostream & operator<<(std::ostream &os, const SquareMatrix &mat)
puts a matrix linewise in output stream
Definition operator.h:376
bool operator==(const SquareMatrix &rhs) const
two Matrices are considered equal if elements are equal
Definition operator.h:221
SquareMatrix & operator+=(const SquareMatrix &op)
add
Definition operator.h:242
void swap_lines(const size_t i, const size_t k)
Swap two lines in the square matrix.
Definition operator.h:142
void zero()
Assign zero to all elements.
Definition operator.h:80
T value_type
typically double or float
Definition operator.h:33
void symv(const ContainerType1 &x, ContainerType2 &y) const
Matrix vector multiplication .
Definition operator.h:175
friend ContainerType operator*(const SquareMatrix &S, const ContainerType &x)
matrix-vector multiplication
Definition operator.h:362
friend SquareMatrix operator*(const T &value, const SquareMatrix &rhs)
scalar multiplication
Definition operator.h:310
SquareMatrix(const std::vector< T > &src)
Copy from existing data.
Definition operator.h:70
SquareMatrix operator-() const
subtract
Definition operator.h:228
friend SquareMatrix operator+(const SquareMatrix &lhs, const SquareMatrix &rhs)
add
Definition operator.h:282
friend SquareMatrix operator-(const SquareMatrix &lhs, const SquareMatrix &rhs)
subtract
Definition operator.h:296
bool operator!=(const SquareMatrix &rhs) const
two Matrices are considered equal if elements are equal
Definition operator.h:209
friend Istream & operator>>(Istream &is, SquareMatrix< T > &mat)
Read values into a Matrix from given istream.
Definition operator.h:398
T & operator()(const size_t i, const size_t j)
access operator
Definition operator.h:91
SquareMatrix()=default
Construct empty SquareMatrix.
std::vector< value_type > & data()
Write access to underlying data.
Definition operator.h:134
SquareMatrix(InputIterator first, InputIterator last, std::enable_if_t<!std::is_integral< InputIterator >::value > *=0)
Construct from iterators.
Definition operator.h:59
SquareMatrix transpose() const
Transposition.
Definition operator.h:156
The discrete legendre trafo class.
#define _ping_
Definition exceptions.h:12
void rhs(double t, double, double &yp)
auto dot(const ContainerType1 &x, const ContainerType2 &y)
Binary reproducible Euclidean dot product between two vectors
Definition blas1.h:153
dg::SquareMatrix< T > inverse(const dg::SquareMatrix< T > &in)
Invert a square matrix.
Definition operator.h:513
void lu_solve(const dg::SquareMatrix< T > &lu, const std::vector< unsigned > &p, std::vector< T > &b)
Solve the linear system with the LU decomposition.
Definition operator.h:681
T lu_pivot(dg::SquareMatrix< T > &m, std::vector< unsigned > &p)
LU Decomposition with partial pivoting.
Definition operator.h:440
dg::SquareMatrix< T > invert(const dg::SquareMatrix< T > &in)
Compute inverse of square matrix (alias for dg::create::inverse)
Definition operator.h:690
double lhs(double x, double y)
This is the namespace for all functions and classes defined and used by the discontinuous Galerkin li...
Indicates that the type has a member function with the same name and interface (up to the matrix itse...
Definition matrix_categories.h:26
Indicate sequential execution.
Definition execution_policy.h:26
T value_type
Definition operator.h:415
The vector traits.
Definition tensor_traits.h:38