/
gsMvMonomialBasis.h
326 lines (254 loc) · 9.07 KB
/
gsMvMonomialBasis.h
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/** @file gsMvMonomialBasis.h
@brief Provides dense multivariate monomial basis.
This file is part of the G+Smo library.
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
Author(s): A. Mantzaflaris
*/
#pragma once
#include <gsCore/gsLinearAlgebra.h>
#include <gsCore/gsBasisFun.h>
#include <gsCore/gsDebug.h>
#include <gsCore/gsBoundary.h>
#include <gsCore/gsBasis.h>
#include <gsUtils/gsCombinatorics.h>
namespace gismo
{
template<short_t d,class T>
class gsMvMonomialBasis : public gsBasis<T>
{
public:
#define Eigen gsEigen
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
#undef Eigen
public:
/// Shared pointer for gsMvMonomialBasis
typedef memory::shared_ptr< gsMvMonomialBasis > Ptr;
/// Unique pointer for gsMvMonomialBasis
typedef memory::unique_ptr< gsMvMonomialBasis > uPtr;
typedef T Scalar_t;
static const bool IsRational = false;
typedef memory::unique_ptr< gsDomainIterator<T> > domainIter;
/// Dimension of the parameter domain
static const short_t Dim = d;
private:
typedef gsEigen::internal::variable_if_dynamic<unsigned,d> dimType;
dimType m_d;
short_t m_degree;
std::vector<gsVector<index_t> > m_compositions;
public:
gsMvMonomialBasis() : m_d(-1), m_degree(-1) { }
gsMvMonomialBasis(unsigned _d, unsigned p) : m_d(_d), m_degree(p)
{
getCompositions(m_compositions);
}
// enable_if<d"=-1>
gsMvMonomialBasis(unsigned p) : m_d(d), m_degree(p)
{
getCompositions(m_compositions);
}
/// Destructor
~gsMvMonomialBasis() { };
public:
// Look at gsBasis class for a description
short_t domainDim() const;
// Look at gsBasis class for a description
void active_into(const gsMatrix<T> & u, gsMatrix<index_t>& result) const
{
const int sz = size();
result.resize( sz, u.cols() );
for ( int i = 0; i< sz; ++i )
result.row(i).setConstant(i);// globally active
}
// Look at gsBasis class for a description
gsMatrix<T> support() const;
// Look at gsBasis class for a description
gsMatrix<T> support(const index_t & i) const;
// Look at gsBasis class for a description
void eval_into(const gsMatrix<T> & u, gsMatrix<T>& result) const;
// Look at gsBasis class for a description
void evalSingle_into(index_t i, const gsMatrix<T> & u, gsMatrix<T>& result) const;
// Look at gsBasis class for a description
void deriv_into(const gsMatrix<T> & u, gsMatrix<T>& result ) const;
// Look at gsBasis class for a description
void derivSingle_into(index_t i, const gsMatrix<T> & u, gsMatrix<T>& result ) const;
// Look at gsBasis class for a description
void deriv2_into(const gsMatrix<T> & u, gsMatrix<T>& result ) const;
// Look at gsBasis class for a description
void deriv2Single_into(index_t i, const gsMatrix<T> & u, gsMatrix<T>& result ) const;
GISMO_CLONE_FUNCTION(gsMvMonomialBasis)
// Look at gsBasis class for a description
memory::unique_ptr<gsGeometry<T> > makeGeometry(gsMatrix<T> coefs ) const
{ GISMO_NO_IMPLEMENTATION }
// Look at gsBasis class for a description
domainIter makeDomainIterator() const
{ GISMO_NO_IMPLEMENTATION }
// Look at gsBasis class for a description
std::ostream &print(std::ostream &os) const
{
os <<"Multivariate monomial basis basis of degree "
<< m_degree <<" and "<<m_d.value()<<" variables\n";
return os;
}
/// Prints the object as a string with extended details.
std::string detail() const
{
// By default just uses print(..)
std::ostringstream os;
print(os);
return os.str();
}
/*
Member functions that may be implemented or not in the derived class
*/
/// The number of basis functions in this basis.
index_t size() const;
short_t totalDegree() const
{ return m_degree; }
short_t degree(short_t i = 0) const
{ return m_degree; }
int degreeOf(index_t k) const
{ return m_compositions[k].sum(); }
private:
//create compositions for basis functions for m_degree
void getCompositions( std::vector<gsVector<index_t> > & compos)const;
// create compositions for basis functions for arbitrary degree
void getCompositions( std::vector<gsVector<index_t> > & compos, index_t degree)const;
//rt derivative
//void rThDerivSingle(index_t r,index_t i, const gsMatrix<T> & dir, const gsMatrix<T> & u, gsMatrix<T>& result)const;
///find p in \a compos and return the index
int findIndex(gsVector<index_t> const p, std::vector<gsVector<index_t> >const compos )const;
}; // class gsMvMonomialBasis
template<short_t d,class T>
short_t gsMvMonomialBasis<d,T>::domainDim() const { return m_d.value(); }
template<short_t d,class T>
index_t gsMvMonomialBasis<d,T>::size() const{
return m_compositions.size();
}
template<short_t d,class T>
gsMatrix<T> gsMvMonomialBasis<d,T>::support(const index_t & i) const
{return gsMatrix<T>();}
template<short_t d,class T>
gsMatrix<T> gsMvMonomialBasis<d,T>:: support() const
{return gsMatrix<T>();}
template<short_t d,class T>
void gsMvMonomialBasis<d,T>::evalSingle_into(index_t i,
const gsMatrix<T> & u,
gsMatrix<T>& result) const
{
gsVector<T> point;
result.setZero(1,u.cols() );
for(int j = 0; j < u.cols(); j++)
{
point = u.col(j);
T val = 1;
for(int k = 0; k < m_compositions[i].size(); k++)
{
val = val * math::pow(point[k], static_cast<int>(m_compositions[i][k]));
}
result.at(j) = val;
}
}
template<short_t d,class T>
void gsMvMonomialBasis<d,T>::eval_into(const gsMatrix<T> & u, gsMatrix<T>& result) const
{
result.setZero(m_compositions.size(), u.cols());
gsMatrix<T> single_result;
for(unsigned i = 0; i < m_compositions.size(); i++)
{
evalSingle_into(i, u, single_result);
result.row(i) = single_result.row(0);
}
}
template<short_t d,class T>
void gsMvMonomialBasis<d,T>::derivSingle_into(index_t i, const gsMatrix<T> & u, gsMatrix<T>& result ) const
{
GISMO_NO_IMPLEMENTATION
}
template<short_t d,class T>
void gsMvMonomialBasis<d,T>::deriv_into(const gsMatrix<T> & u, gsMatrix<T>& result ) const
{
result.setZero(m_compositions.size()*this->dim(), u.cols());
gsMatrix<T> single_result;
for(unsigned i = 0; i < m_compositions.size(); i++)
{
derivSingle_into(i, u, single_result);
for(int j = 0; j < single_result.rows();j++)
{
result.row(m_d.value()*i+j) = single_result.row(j); // Map
}
}
}
template<short_t d,class T>
void gsMvMonomialBasis<d,T>::deriv2Single_into(index_t i, const gsMatrix<T> & u,
gsMatrix<T>& result ) const
{
result.setZero(m_d.value(), u.cols());
gsVector<T> point;
for(index_t j = 0; j < u.cols(); ++j)
{
point = u.col(j);
for(index_t l = 0; l < m_compositions[i].size(); ++l)
{
T val = 1;
for(index_t k = 0; k < m_compositions[i].size(); ++k)
{
const unsigned ex = m_compositions[i][k];
if (ex<1 ) { val=0; break; }
if (ex==1) { continue; }
val = val * ex * math::pow(point[k], static_cast<int>(ex-1));
}
result(l,j) = val;
}
}
}
template<short_t d,class T>
void gsMvMonomialBasis<d,T>::deriv2_into(const gsMatrix<T> & u, gsMatrix<T>& result ) const
{
result.setZero(m_compositions.size()*((d * (d + 1)) / 2), u.cols());
gsMatrix<T> single_result;
for(unsigned i = 0; i < m_compositions.size(); i++)
{
deriv2Single_into(i, u, single_result);
for(int j = 0; j < single_result.rows();j++){
result.row(single_result.rows()*i+j) = single_result.row(j);
}
}
}
template<short_t d,class T>
void gsMvMonomialBasis<d,T>::getCompositions(std::vector<gsVector<index_t> > & compos)const{
this->getCompositions(compos,m_degree);
}
namespace
{
bool comp_deg(const gsVector<index_t> & a, const gsVector<index_t>& b)
{ return a.sum()<b.sum(); }
}
template<short_t d,class T>
void gsMvMonomialBasis<d,T>::getCompositions(std::vector<gsVector<index_t> > & compos, index_t degree)const
{
compos.reserve(numCompositions(degree,m_d.value()+1));
gsVector<index_t> RR;
firstComposition(degree,m_d.value()+1,RR);
do
{
compos.push_back(RR.tail(m_d.value()));
} while ( nextComposition(RR) );
std::sort(compos.begin(), compos.end(), comp_deg );
}
template<short_t d,class T>
int gsMvMonomialBasis<d,T>::findIndex(gsVector<index_t> const p,
std::vector<gsVector<index_t> >const compos )const
{
//std::find
for(index_t i = 0; i < compos.size();i++)
{
if(p==compos[i])return i;
}
return -1;
}
}; // namespace gismo
// #ifndef GISMO_BUILD_LIB
// #include GISMO_HPP_HEADER(gsMvMonomialBasis.hpp)
// #endif