G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsMonomialBasis.hpp
1 #pragma once
2 
3 #include <gsPolynomial/gsMonomialBasis.h>
4 #include <math.h>
6 
7 namespace gismo
8 {
9 
10 template <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 
19 template <class T>
21 {
22  return 1; // Since we consider univariate basis
23 }
24 
25 template <class T>
26 std::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 
34 template <class T>
36 {
37  return m_p+1;
38 }
39 
40 template <class T>
41 memory::unique_ptr<gsGeometry<T> > gsMonomialBasis<T> :: makeGeometry(gsMatrix<T> ) const
42 {
44 }
45 
46 template <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 
60 template <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 
72 template <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 
88 template <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 
104 template <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 
122 template <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 
138 template <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 
160 template <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 
184 template <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 
206 template <class T>
207 void 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 
gsMonomialBasis()
Default constructor, which sets the degree to 0.
Definition: gsMonomialBasis.h:36
#define GISMO_NO_IMPLEMENTATION
Definition: gsDebug.h:129
virtual index_t size() const
Returns the numer of basis functions.
Definition: gsMonomialBasis.hpp:35
#define short_t
Definition: gsConfig.h:35
virtual void evalAllDersSingle_into(index_t i, const gsMatrix< T > &u, int n, gsMatrix< T > &result) const
Evaluates all derivatives up to order n of the i-th basis function at values u.
Definition: gsMonomialBasis.hpp:161
virtual short_t domainDim() const
Returns the dimension of the parameter domain.
Definition: gsMonomialBasis.hpp:20
#define index_t
Definition: gsConfig.h:32
#define GISMO_ENSURE(cond, message)
Definition: gsDebug.h:102
virtual void deriv_into(const gsMatrix< T > &u, gsMatrix< T > &result) const
Evaluates the derivative of the basis functions at values u.
Definition: gsMonomialBasis.hpp:73
Provides combinatorial unitilies.
virtual void eval_into(const gsMatrix< T > &u, gsMatrix< T > &result) const
Evaluates the basis functions at values u.
Definition: gsMonomialBasis.hpp:47
virtual std::ostream & print(std::ostream &os) const
Prints the object as a string.
Definition: gsMonomialBasis.hpp:26
virtual void deriv2_into(const gsMatrix< T > &u, gsMatrix< T > &result) const
Evaluates the second derivative of the basis functions at values u.
Definition: gsMonomialBasis.hpp:105
virtual void evalAllDers_into(const gsMatrix< T > &u, int n, gsMatrix< T > &result) const
Evaluates all derivatives up to order n of the basis functions at values u.
Definition: gsMonomialBasis.hpp:139
virtual void evalDerSingle_into(index_t i, const gsMatrix< T > &u, int n, gsMatrix< T > &result) const
Evaluates the derivative of order n of the i-th basis function at values u.
Definition: gsMonomialBasis.hpp:185
virtual void deriv2Single_into(index_t i, const gsMatrix< T > &u, gsMatrix< T > &result) const
Evaluates the second derivatives of the i-th basis function at values u.
Definition: gsMonomialBasis.hpp:123
virtual void evalSingle_into(index_t i, const gsMatrix< T > &u, gsMatrix< T > &result) const
Evaluates the i-th basis function at values u.
Definition: gsMonomialBasis.hpp:61
virtual void evalFunc_into(const gsMatrix< T > &u, const gsMatrix< T > &coefs, gsMatrix< T > &result) const
Evaluate the function described by coefs at points u, i.e., evaluates a linear combination of coefs x...
Definition: gsMonomialBasis.hpp:207
unsigned factorial(unsigned n)
Returns the factorial of n i.e. n! Remember that factorial grow too fast and only n! with n&lt;=13 can b...
Definition: gsCombinatorics.h:29
virtual void derivSingle_into(index_t i, const gsMatrix< T > &u, gsMatrix< T > &result) const
Evaluates the derivatives of the i-th basis function at values u.
Definition: gsMonomialBasis.hpp:89
virtual memory::unique_ptr< gsGeometry< T > > makeGeometry(gsMatrix< T > coefs) const
Create a gsGeometry of proper type for this basis with the given coefficient matrix.
Definition: gsMonomialBasis.hpp:41