-
Notifications
You must be signed in to change notification settings - Fork 1
/
gsMonomialBasis.hpp
225 lines (176 loc) · 7.22 KB
/
gsMonomialBasis.hpp
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
#pragma once
#include <gsPolynomial/gsMonomialBasis.h>
#include <math.h>
#include <gsUtils/gsCombinatorics.h>
namespace gismo
{
template <class T>
gsMonomialBasis<T> :: gsMonomialBasis(int degree)
{
if(degree>=0)
m_p=degree;
else
std::cout<<"Error: The degree has to be >=0"<<std::endl;
}
template <class T>
short_t gsMonomialBasis<T> :: domainDim() const
{
return 1; // Since we consider univariate basis
}
template <class T>
std::ostream & gsMonomialBasis<T> :: print(std::ostream &os) const
{
os<<"gsMonomialBasis"<<std::endl;
os<<"degree: "<<m_p<<std::endl;
os<<"size: "<<size()<<std::endl;
return os;
}
template <class T>
index_t gsMonomialBasis<T> :: size() const
{
return m_p+1;
}
template <class T>
memory::unique_ptr<gsGeometry<T> > gsMonomialBasis<T> :: makeGeometry(gsMatrix<T> ) const
{
GISMO_NO_IMPLEMENTATION;
}
template <class T>
void gsMonomialBasis<T> :: eval_into(const gsMatrix<T> & u, gsMatrix<T>& result) const
{
result.resize(size(), u.cols());
// 0th basis function is constant 1
result.row(0).setOnes();
for(int p=1; p<=m_p; p++) // For all monomials up to m_p
for(int eval_point=0; eval_point<u.cols(); eval_point++) // For all values (columns of u)
result(p,eval_point)=result(p-1,eval_point)*u(0,eval_point);
}
template <class T>
void gsMonomialBasis<T> :: evalSingle_into(index_t i, const gsMatrix<T> & u, gsMatrix<T>& result) const
{
GISMO_ENSURE(static_cast<int>(i)<=m_p,"Error: There is no i-th basis function in the basis."<<std::endl<<
"Number of basis functions in the basis is "<<size()<<".");
result.resize(1,u.cols());
for(int eval_point=0; eval_point<u.cols(); eval_point++) // For all values (columns of u)
result(0,eval_point) = math::pow(u(0,eval_point),static_cast<int>(i));
}
template <class T>
void gsMonomialBasis<T> :: deriv_into(const gsMatrix<T> & u, gsMatrix<T>& result ) const
{
result.resize(size(), u.cols());
// Derivative 0th basis function is constant 0
result.row(0).setZero();
// Derivative 1st basis function is constant 1
if(m_p>=1)
result.row(1).setOnes();
for(int p=2; p<=m_p; p++) // For all monomials up to m_p
for(int eval_point=0; eval_point<u.cols(); eval_point++) // For all values (columns of u)
result(p,eval_point)=(real_t)p/(p-1)*result(p-1,eval_point)*u(0,eval_point);
}
template <class T>
void gsMonomialBasis<T> :: derivSingle_into(index_t i, const gsMatrix<T> & u, gsMatrix<T>& result ) const
{
GISMO_ENSURE(static_cast<int>(i)<=m_p,"Error: There is no i-th basis function in the basis."<<std::endl<<
"Number of basis functions in the basis is "<<size()<<".");
result.resize(1,u.cols());
if(i==0)
result.setZero(1,u.cols());
else
for(int eval_point=0; eval_point<u.cols(); eval_point++) // For all values (columns of u)
result(0,eval_point)=i* math::pow(u(0,eval_point),static_cast<int>(i)-1);
}
template <class T>
void gsMonomialBasis<T> :: deriv2_into(const gsMatrix<T> & u, gsMatrix<T>& result ) const
{
result.resize(size(), u.cols());
// 2nd derivative of 0th and 1st basis function is constant 0
result.row(0).setZero();
if(m_p>=1)
result.row(1).setZero();
// 2nd derivative of 2nd basis function is constant 2
if(m_p>=2)
result.row(2).setConstant(2);
for(int p=3; p<=m_p; p++) // For all monomials up to m_p
for(int eval_point=0; eval_point<u.cols(); eval_point++) // For all values (columns of u)
result(p,eval_point)=(real_t)p/(p-2)*result(p-1,eval_point)*u(0,eval_point);
}
template <class T>
void gsMonomialBasis<T> :: deriv2Single_into(index_t i, const gsMatrix<T> & u, gsMatrix<T>& result ) const
{
GISMO_ENSURE(static_cast<int>(i)<=m_p,"Error: There is no i-th basis function in the basis."<<std::endl<<
"Number of basis functions in the basis is "<<size()<<".");
result.resize(1,u.cols());
if(i==0 || i==1)
result.setZero(1,u.cols());
else
for(int eval_point=0; eval_point<u.cols(); eval_point++)
result(0,eval_point)=i*(i-1) * math::pow(u(0,eval_point),static_cast<int>(i)-2);
}
template <class T>
void gsMonomialBasis<T> :: evalAllDers_into(const gsMatrix<T> & u, int n, gsMatrix<T>& result) const
{
result.resize(size()*(n+1),u.cols());
for (int deriv_order=0; deriv_order<=n; deriv_order++)
{
// Basis functions whose derivative is already zero
for(int p=0; p<deriv_order && p<=m_p; p++)
result.row(deriv_order*size()+p).setZero();
// Basis function whose derivative is constant
if(deriv_order<=m_p)
result.row(deriv_order*size()+deriv_order).setConstant(factorial(deriv_order));
// Remaining basis functions whose derivative is not constant
for(int p=deriv_order+1; p<=m_p; p++)
for(int eval_point=0; eval_point<u.cols(); eval_point++)
result(deriv_order*size()+p,eval_point)=(real_t)p/(p-deriv_order)*result(deriv_order*size()+p-1,eval_point)*u(0,eval_point);
}
}
template <class T>
void gsMonomialBasis<T> :: evalAllDersSingle_into(index_t i, const gsMatrix<T> & u, int n, gsMatrix<T>& result) const
{
GISMO_ENSURE(static_cast<int>(i)<=m_p,"Error: There is no i-th basis function in the basis."<<std::endl<<
"Number of basis functions in the basis is "<<size()<<".");
result.resize(n+1, u.cols());
// Compute function values
for(int eval_point=0; eval_point<u.cols(); eval_point++)
result(0,eval_point) = math::pow(u(0,eval_point),static_cast<int>(i));
// Compute derivatives up to order n
for (int deriv_order=1; deriv_order<=n; deriv_order++)
{
if(static_cast<int>(i)<deriv_order)
result.row(deriv_order).setZero();
else
for(int eval_point=0; eval_point<u.cols(); eval_point++)
result(deriv_order,eval_point)=(i+1-deriv_order)*result(deriv_order-1,eval_point)/u(0,eval_point);
}
}
template <class T>
void gsMonomialBasis<T> :: evalDerSingle_into(index_t i, const gsMatrix<T> & u, int n, gsMatrix<T>& result) const
{
GISMO_ENSURE(static_cast<int>(i)<=m_p,"Error: There is no i-th basis function in the basis."<<std::endl<<
"Number of basis functions in the basis is "<<size()<<".");
result.resize(1, u.cols());
// Compute factor resulting from derivation
real_t factor=1;
for(int j=0; j<=n-1; j++)
factor*=i-j;
if((int)i<n)
result.setZero();
else
for(int eval_point=0; eval_point<u.cols(); eval_point++)
result(0,eval_point)=factor * math::pow(u(0,eval_point),static_cast<int>(i)-n);
}
template <class T>
void gsMonomialBasis<T> :: evalFunc_into(const gsMatrix<T> & u, const gsMatrix<T> & coefs, gsMatrix<T>& result) const
{
result.resize(coefs.cols(),u.cols());
int n_coefs=coefs.rows();
// Loop over the evaluation points
for(int eval_point=0; eval_point<u.cols(); eval_point++)
{
// Horner Scheme
result.col(eval_point)=coefs.row(n_coefs-1).transpose();
for(int i=n_coefs-2; i>=0; i--)
result.col(eval_point)=result.col(eval_point)*u.col(eval_point)+coefs.row(i).transpose();
}
}
}