5#ifndef DUNE_MULTIINDEX_HH
6#define DUNE_MULTIINDEX_HH
14#include <dune/localfunctions/utility/field.hh>
25 template<
int dim,
class Field >
28 template<
int dim,
class Field >
29 std::ostream &operator<< ( std::ostream &,
const MultiIndex< dim,Field > & );
36 template<
int dim,
class Field >
39 typedef MultiIndex< dim, Field > This;
41 friend std::ostream &operator<<<> ( std::ostream &,
const This & );
44 static const int dimension = dim;
53 explicit MultiIndex (
const F &f)
60 MultiIndex (
int,
const This &other )
61 : vecZ_( other.vecOMZ_ ),
62 vecOMZ_( other.vecZ_ ),
63 factor_( other.factor_ )
68 next_ =
new This( *(other.next_) );
74 MultiIndex (
const This &other )
75 : vecZ_( other.vecZ_ ),
76 vecOMZ_( other.vecOMZ_ ),
77 factor_( other.factor_ )
81 next_ =
new This( *(other.next_) );
100 const Field &factor()
const
105 This &operator= (
const This &other )
109 vecOMZ_ = other.vecOMZ_;
110 factor_ = other.factor_;
112 next_ =
new This(*(other.next_));
115 This &operator= (
const Zero<This> &f )
123 This &operator= (
const Unity<This> &f )
132 This &operator= (
const F &f )
137 factor_ = field_cast<Field>(f);
143 assert(!next_ && !other.next_);
144 return (vecZ_==other.vecZ_ && vecOMZ_==other.vecOMZ_ && factor_==other.factor_);
148 This &operator*= (
const F &f )
150 factor_ *= field_cast<Field>(f);
156 This &operator/= (
const F &f )
158 factor_ /= field_cast<Field>(f);
164 This &operator*= (
const This &other )
166 assert(!other.next_);
167 vecZ_ += other.vecZ_;
168 vecOMZ_ += other.vecOMZ_;
169 factor_ *= other.factor_;
174 This &operator/= (
const This &other )
176 assert(!other.next_);
177 vecZ_ -= other.vecZ_;
178 vecOMZ_ -= other.vecOMZ_;
179 factor_ /= other.factor_;
185 This &operator+= (
const This &other )
187 assert(!other.next_);
188 if (std::abs(other.factor_)<1e-10)
190 if (std::abs(factor_)<1e-10)
195 if (!sameMultiIndex(other))
201 next_ =
new This(other);
205 factor_ += other.factor_;
208 This &operator-= (
const This &other )
210 assert(!other.next_);
211 if (!sameMultiIndex(other))
217 next_ =
new This(other);
221 factor_ -= other.factor_;
226 This operator* (
const F &f )
const
232 This operator/ (
const F &f )
const
238 This operator* (
const This &other )
const
243 This operator/ (
const This &other )
const
249 This operator+ (
const This &other )
const
254 This operator- (
const This &other )
const
260 void set (
int d,
int power = 1 )
268 for(
int i = 0; i < dimension; ++i )
269 ret += std::abs( vecZ_[ i ] );
276 for(
int i = 0; i < dimension; ++i )
277 ret += std::abs( vecOMZ_[ i ] );
281 bool sameMultiIndex(
const This &ind)
283 for(
int i = 0; i < dimension; ++i )
285 if ( vecZ_[i] != ind.vecZ_[i] ||
286 vecOMZ_[i] != vecOMZ_[i] )
313 template<
int dim,
class Field >
314 struct FieldTraits<MultiIndex<dim,Field>>
316 using field_type = Field;
317 using real_type =
typename FieldTraits<field_type>::real_type;
320 template <
int dim,
class Field,
class F>
321 MultiIndex<dim,Field> operator* (
const F &f,
322 const MultiIndex<dim,Field> &m)
324 MultiIndex<dim,Field> z = m;
327 template <
int dim,
class Field,
class F>
328 MultiIndex<dim,Field> operator/ (
const F &f,
329 const MultiIndex<dim,Field> &m)
331 MultiIndex<dim,Field> z = m;
335 template <
int d,
class F>
336 std::ostream &operator<<(std::ostream& out,
const std::vector<MultiIndex<d,F> >& y) {
337 for (
unsigned int r=0; r<y.size(); ++r) {
338 out <<
"f_{" << r <<
"}(" << char(
'a');
339 for (
int i=1; i<d; ++i)
340 out <<
"," <<
char(
'a'+i);
342 out << y[r] << std::endl;
346 template <
int d,
class F,
int dimR>
347 std::ostream &operator<<(std::ostream& out,
349 out <<
"\\begin{eqnarray*}" << std::endl;
350 for (
unsigned int k=0; k<y.size(); ++k) {
351 out <<
"f_{" << k <<
"}(" << char(
'a');
352 for (
int i=1; i<d; ++i)
353 out <<
"," <<
char(
'a'+i);
356 for (
unsigned int r=1; r<dimR; ++r) {
357 out <<
" , " << y[k][r] ;
359 out <<
" ) \\\\" << std::endl;
361 out <<
"\\end{eqnarray*}" << std::endl;
364 template <
int d,
class F,
int dimR1,
int dimR2>
365 std::ostream &operator<<(std::ostream& out,
367 out <<
"\\begin{eqnarray*}" << std::endl;
368 for (
unsigned int k=0; k<y.size(); ++k) {
369 for (
int q=0; q<dimR2; q++) {
370 out <<
"d_{" << char(
'a'+q) <<
"}f_{" << k <<
"}(" << char(
'a');
371 for (
int i=1; i<d; ++i)
372 out <<
"," <<
char(
'a'+i);
375 for (
unsigned int r=1; r<dimR1; ++r) {
376 out <<
" , " << y[k][r][q] ;
378 out <<
" ) \\\\" << std::endl;
381 out <<
"\\end{eqnarray*}" << std::endl;
384 template <
int d,
class F>
385 std::ostream &operator<<(std::ostream& out,
const MultiIndex<d,F>& val)
388 const MultiIndex<d,F> *m = &val;
390 if (m->absZ()==0 && std::abs(m->factor())<1e-10)
392 if (!m->next_ || !first)
402 if (m->factor()>0 && !first)
404 else if (m->factor()<0)
412 F f = std::abs(m->factor());
416 if ( std::abs(f)<1e-10)
422 if ( std::abs(f_1)>1e-10)
425 for (
int i=0; i<d; ++i) {
428 else if (m->vecZ_[i]==1)
430 else if (m->vecZ_[i]>0)
431 out << char(
'a'+i) <<
"^" << m->vecZ_[i] <<
"";
432 else if (m->vecZ_[i]<0)
433 out << char(
'a'+i) <<
"^" << m->vecZ_[i] <<
"";
434 absVal += m->vecZ_[i];
435 if (absVal<m->absZ()) out <<
"";
459 template<
int dim,
class F>
460 struct Unity< MultiIndex< dim, F > >
462 typedef MultiIndex< dim, F > Field;
464 operator Field ()
const
469 Field operator- (
const Field &other )
const
471 return Field( 1, other );
474 Field operator/ (
const Field &other )
const
476 return Field() / other;
482 template<
int dim,
class F >
483 struct Zero< MultiIndex< dim,F > >
485 typedef MultiIndex< dim,F > Field;
494 template<
int dim,
class Field >
495 bool operator< (
const Zero< MultiIndex< dim,Field > > &,
const MultiIndex< dim,Field > & )
500 template<
int dim,
class Field >
501 bool operator< (
const MultiIndex< dim, Field > &f,
const Zero< MultiIndex< dim,Field > > & )
A dense n x m matrix.
Definition: fmatrix.hh:117
Type traits to determine the type of reals (when working with complex numbers)
Implements a vector constructed from a given type representing a field and a compile-time given size.
EnableIfInterOperable< T1, T2, bool >::type operator<(const RandomAccessIteratorFacade< T1, V1, R1, D > &lhs, const RandomAccessIteratorFacade< T2, V2, R2, D > &rhs)
Comparison operator.
Definition: iteratorfacades.hh:638
EnableIfInterOperable< T1, T2, bool >::type operator==(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for equality.
Definition: iteratorfacades.hh:238
Dune namespace.
Definition: alignedallocator.hh:13
void field_cast(const F1 &f1, F2 &f2)
a helper class to cast from one field to another
Definition: field.hh:159
constexpr Base power(Base m, Exponent p)
Power method for integer exponents.
Definition: math.hh:75