G+Smo  25.01.0
Geometry + Simulation Modules
 
Loading...
Searching...
No Matches
gsMonomialBasis.hpp
1#pragma once
2
3#include <gsPolynomial/gsMonomialBasis.h>
4#include <math.h>
6
7namespace gismo
8{
9
10template <class T>
12{
13 if(degree>=0)
14 m_p=degree;
15 else
16 std::cout<<"Error: The degree has to be >=0"<<std::endl;
17}
18
19template <class T>
21{
22 return 1; // Since we consider univariate basis
23}
24
25template <class T>
26std::ostream & gsMonomialBasis<T> :: print(std::ostream &os) const
27{
28 os<<"gsMonomialBasis"<<std::endl;
29 os<<"degree: "<<m_p<<std::endl;
30 os<<"size: "<<size()<<std::endl;
31 return os;
32}
33
34template <class T>
36{
37 return m_p+1;
38}
39
40template <class T>
41memory::unique_ptr<gsGeometry<T> > gsMonomialBasis<T> :: makeGeometry(gsMatrix<T> ) const
42{
44}
45
46template <class T>
48{
49 result.resize(size(), u.cols());
50
51 // 0th basis function is constant 1
52 result.row(0).setOnes();
53
54 for(int p=1; p<=m_p; p++) // For all monomials up to m_p
55 for(int eval_point=0; eval_point<u.cols(); eval_point++) // For all values (columns of u)
56 result(p,eval_point)=result(p-1,eval_point)*u(0,eval_point);
57
58}
59
60template <class T>
62{
63 GISMO_ENSURE(static_cast<int>(i)<=m_p,"Error: There is no i-th basis function in the basis."<<std::endl<<
64 "Number of basis functions in the basis is "<<size()<<".");
65
66 result.resize(1,u.cols());
67
68 for(int eval_point=0; eval_point<u.cols(); eval_point++) // For all values (columns of u)
69 result(0,eval_point) = math::pow(u(0,eval_point),static_cast<int>(i));
70}
71
72template <class T>
74{
75 result.resize(size(), u.cols());
76
77 // Derivative 0th basis function is constant 0
78 result.row(0).setZero();
79 // Derivative 1st basis function is constant 1
80 if(m_p>=1)
81 result.row(1).setOnes();
82
83 for(int p=2; p<=m_p; p++) // For all monomials up to m_p
84 for(int eval_point=0; eval_point<u.cols(); eval_point++) // For all values (columns of u)
85 result(p,eval_point)=(real_t)p/(p-1)*result(p-1,eval_point)*u(0,eval_point);
86}
87
88template <class T>
90{
91 GISMO_ENSURE(static_cast<int>(i)<=m_p,"Error: There is no i-th basis function in the basis."<<std::endl<<
92 "Number of basis functions in the basis is "<<size()<<".");
93
94 result.resize(1,u.cols());
95
96 if(i==0)
97 result.setZero(1,u.cols());
98 else
99 for(int eval_point=0; eval_point<u.cols(); eval_point++) // For all values (columns of u)
100 result(0,eval_point)=i* math::pow(u(0,eval_point),static_cast<int>(i)-1);
101
102}
103
104template <class T>
106{
107 result.resize(size(), u.cols());
108
109 // 2nd derivative of 0th and 1st basis function is constant 0
110 result.row(0).setZero();
111 if(m_p>=1)
112 result.row(1).setZero();
113 // 2nd derivative of 2nd basis function is constant 2
114 if(m_p>=2)
115 result.row(2).setConstant(2);
116
117 for(int p=3; p<=m_p; p++) // For all monomials up to m_p
118 for(int eval_point=0; eval_point<u.cols(); eval_point++) // For all values (columns of u)
119 result(p,eval_point)=(real_t)p/(p-2)*result(p-1,eval_point)*u(0,eval_point);
120}
121
122template <class T>
124{
125 GISMO_ENSURE(static_cast<int>(i)<=m_p,"Error: There is no i-th basis function in the basis."<<std::endl<<
126 "Number of basis functions in the basis is "<<size()<<".");
127
128 result.resize(1,u.cols());
129
130 if(i==0 || i==1)
131 result.setZero(1,u.cols());
132 else
133 for(int eval_point=0; eval_point<u.cols(); eval_point++)
134 result(0,eval_point)=i*(i-1) * math::pow(u(0,eval_point),static_cast<int>(i)-2);
135
136}
137
138template <class T>
140{
141 result.resize(size()*(n+1),u.cols());
142
143 for (int deriv_order=0; deriv_order<=n; deriv_order++)
144 {
145 // Basis functions whose derivative is already zero
146 for(int p=0; p<deriv_order && p<=m_p; p++)
147 result.row(deriv_order*size()+p).setZero();
148
149 // Basis function whose derivative is constant
150 if(deriv_order<=m_p)
151 result.row(deriv_order*size()+deriv_order).setConstant(factorial(deriv_order));
152
153 // Remaining basis functions whose derivative is not constant
154 for(int p=deriv_order+1; p<=m_p; p++)
155 for(int eval_point=0; eval_point<u.cols(); eval_point++)
156 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);
157 }
158}
159
160template <class T>
162{
163 GISMO_ENSURE(static_cast<int>(i)<=m_p,"Error: There is no i-th basis function in the basis."<<std::endl<<
164 "Number of basis functions in the basis is "<<size()<<".");
165
166 result.resize(n+1, u.cols());
167
168 // Compute function values
169 for(int eval_point=0; eval_point<u.cols(); eval_point++)
170 result(0,eval_point) = math::pow(u(0,eval_point),static_cast<int>(i));
171
172 // Compute derivatives up to order n
173 for (int deriv_order=1; deriv_order<=n; deriv_order++)
174 {
175 if(static_cast<int>(i)<deriv_order)
176 result.row(deriv_order).setZero();
177 else
178 for(int eval_point=0; eval_point<u.cols(); eval_point++)
179 result(deriv_order,eval_point)=(i+1-deriv_order)*result(deriv_order-1,eval_point)/u(0,eval_point);
180 }
181
182}
183
184template <class T>
186{
187 GISMO_ENSURE(static_cast<int>(i)<=m_p,"Error: There is no i-th basis function in the basis."<<std::endl<<
188 "Number of basis functions in the basis is "<<size()<<".");
189
190 result.resize(1, u.cols());
191
192 // Compute factor resulting from derivation
193 real_t factor=1;
194 for(int j=0; j<=n-1; j++)
195 factor*=i-j;
196
197 if((int)i<n)
198 result.setZero();
199 else
200 for(int eval_point=0; eval_point<u.cols(); eval_point++)
201 result(0,eval_point)=factor * math::pow(u(0,eval_point),static_cast<int>(i)-n);
202
203}
204
205
206template <class T>
207void gsMonomialBasis<T> :: evalFunc_into(const gsMatrix<T> & u, const gsMatrix<T> & coefs, gsMatrix<T>& result) const
208{
209 result.resize(coefs.cols(),u.cols());
210 int n_coefs=coefs.rows();
211
212 // Loop over the evaluation points
213 for(int eval_point=0; eval_point<u.cols(); eval_point++)
214 {
215 // Horner Scheme
216 result.col(eval_point)=coefs.row(n_coefs-1).transpose();
217 for(int i=n_coefs-2; i>=0; i--)
218 result.col(eval_point)=result.col(eval_point)*u.col(eval_point)+coefs.row(i).transpose();
219 }
220}
221
222
223
224}
225
A matrix with arbitrary coefficient type and fixed or dynamic size.
Definition gsMatrix.h:41
An univariate monomial polynomial basis. If the degree is p the basis is given by: [ 1,...
Definition gsMonomialBasis.h:24
unsigned factorial(unsigned n)
Returns the factorial of n i.e. n! Remember that factorial grow too fast and only n!...
Definition gsCombinatorics.h:29
Provides combinatorial unitilies.
#define short_t
Definition gsConfig.h:35
#define index_t
Definition gsConfig.h:32
#define GISMO_NO_IMPLEMENTATION
Definition gsDebug.h:129
#define GISMO_ENSURE(cond, message)
Definition gsDebug.h:102
The G+Smo namespace, containing all definitions for the library.