Discontinuous Galerkin Library
#include "dg/algorithm.h"
operator_tensor.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4
5#include <cusp/coo_matrix.h>
6#include <cusp/multiply.h>
7#include "operator.h"
8
9namespace dg
10{
11
14
15
26template< class T>
28{
29#ifdef DG_DEBUG
30 assert( op1.size() == op2.size());
31#endif //DG_DEBUG
32 unsigned n = op1.size();
33 Operator<T> prod( n*n);
34 for( unsigned i=0; i<n; i++)
35 for( unsigned j=0; j<n; j++)
36 for( unsigned k=0; k<n; k++)
37 for( unsigned l=0; l<n; l++)
38 prod(i*n+k, j*n+l) = op1(i,j)*op2(k,l);
39 return prod;
40}
41
42
61template< class T>
62cusp::coo_matrix<int,T, cusp::host_memory> tensorproduct( unsigned N, const Operator<T>& op)
63{
64 assert( N>0);
65 unsigned n = op.size();
66 //compute number of nonzeroes in op
67 unsigned number = n*n;
68 // allocate output matrix
69 cusp::coo_matrix<int, T, cusp::host_memory> A(n*N, n*N, N*number);
70 number = 0;
71 for( unsigned k=0; k<N; k++)
72 for( unsigned i=0; i<n; i++)
73 for( unsigned j=0; j<n; j++)
74 //if( op(i,j) != 0)
75 {
76 A.row_indices[number] = k*n+i;
77 A.column_indices[number] = k*n+j;
78 A.values[number] = op(i,j);
79 number++;
80 }
81 return A;
82}
83
84
96template< class T>
97cusp::coo_matrix<int, T, cusp::host_memory> sandwich( const Operator<T>& left, const cusp::coo_matrix<int, T, cusp::host_memory>& m, const Operator<T>& right)
98{
99 assert( left.size() == right.size());
100 typedef cusp::coo_matrix<int, T, cusp::host_memory> Matrix;
101 unsigned n = left.size();
102 unsigned N = m.num_rows/n;
103 Matrix r = tensorproduct( N, right);
104 Matrix l = tensorproduct( N, left);
105 Matrix mr(m ), lmr(m);
106
107 cusp::multiply( m, r, mr);
108 cusp::multiply( l, mr, lmr);
109 return lmr;
110}
111
112
113
115
116
117}//namespace dg
118
119
A square nxn matrix.
Definition: operator.h:28
unsigned size() const
Size n of the Operator.
Definition: operator.h:113
Operator< T > tensorproduct(const Operator< T > &op1, const Operator< T > &op2)
Form the tensor product between two operators.
Definition: operator_tensor.h:27
cusp::coo_matrix< int, T, cusp::host_memory > sandwich(const Operator< T > &left, const cusp::coo_matrix< int, T, cusp::host_memory > &m, const Operator< T > &right)
Multiply 1d matrices by diagonal block matrices from left and right.
Definition: operator_tensor.h:97
This is the namespace for all functions and classes defined and used by the discontinuous Galerkin li...