4 #include <gsPolynomial/gsMonomialBasis.h>
22 const std::string& oldStr,
23 const std::string& newStr)
26 while((pos = str.find(oldStr, pos)) != std::string::npos)
28 str.replace(pos, oldStr.length(), newStr);
29 pos += newStr.length();
35 class gsMonomialPoly :
public gsGeoTraits<1,T>::GeometryBase
38 typedef gsMonomialBasis<T> Basis;
40 typedef typename gsGeoTraits<1,T>::GeometryBase Base;
43 typedef memory::shared_ptr< gsMonomialPoly > Ptr;
46 typedef memory::unique_ptr< gsMonomialPoly > uPtr;
55 explicit gsMonomialPoly(
const gsMatrix<T> & coefs,
int p = -1) :
56 Base(Basis(p==-1?coefs.rows()-1:p), coefs )
59 explicit gsMonomialPoly(
const std::string & str, std::string var =
"x")
60 {this->set_str(str,var);}
63 gsMonomialPoly(
const Basis & basis,
const gsMatrix<T> & coefs ) :
68 virtual std::ostream &print(std::ostream &os)
const
73 os <<
"gsMonomialPoly (constant): "<<
m_coefs.
at(0);
78 os <<
"gsMonomialPoly (deg="<<p<<
"): ";
83 if (i!=p) os << (
m_coefs.
at(i) > 0 ?
"+" :
"-");
85 if (1!=a) os << a <<
"*";
86 if (1!=i) os<<
"x^"<<i;
else os<<
"x";
97 GISMO_CLONE_FUNCTION(gsMonomialPoly)
99 int deg()
const {
return basis().deg();}
104 return (
m_coefs.array() == 0).all();
108 bool isConstant()
const
110 return (
m_coefs.bottomRows(deg()).array() == 0).all();
116 return (1 == leadCoeff()).all();
141 void asBezier(gsBezier<T> & bezier)
const
143 const int p = this->deg();
144 const int n = this->
geoDim();
145 gsBernsteinBasis<T> bbasis(0, 1, p);
147 gsMatrix<T> diff_table(p+1,p+1);
148 gsMatrix<T> coefs_bezier(bbasis.size(), n);
155 diff_table.col(0)=coefs.col(j);
156 for(
int i=1; i<=p-1; i++)
160 for(
int column=p-1; column>=0; column--)
161 for(
int row=1; row<=p-column; row++)
162 diff_table(column, row)=diff_table(column+1,row-1)+diff_table(column,row-1);
165 coefs_bezier.col(j)=diff_table.row(0).transpose();
169 bezier = gsBezier<T>(bbasis,
give(coefs_bezier));
174 void set_str(
const std::string & str, std::string var =
"x");
178 void gsMonomialPoly<T>::set_str(
const std::string & str, std::string var)
181 std::string poly(
" ");
182 for ( std::string::const_iterator it=str.begin(); it!=str.end(); ++it)
186 if (*poly.rbegin() !=
'*' ) poly +=
"1*";
188 if ( (it+1==str.end() || *(it+1)!=
'^') ) poly +=
"^1";
190 else if (*it==
'+') poly +=
" ";
191 else if (*it==
'-') poly +=
" -";
192 else if (*it!=
' ') poly += *it;
197 std::istringstream term, in(poly);
199 std::vector<T> vcf(1,0);
201 std::vector<unsigned> vex(1,0);
206 if (t.find(
"x")!=std::string::npos)
209 term.clear();term.str(t);
210 if (!gsGetValue(term, cf))
gsWarn<<
"Error parsing coefficient.\n";
211 if (!gsGetInt (term, ex))
gsWarn<<
"Error parsing exponent.\n";
217 term.clear();term.str(t);
218 if (!gsGetValue(term, cf))
gsWarn<<
"Error parsing constant coefficient.\n";
226 this->m_basis =
new Basis(*std::max_element(vex.begin(), vex.end()));
227 m_coefs.setZero(deg()+1, 1);
228 for (
size_t i = 0; i!= vex.size(); ++i)
229 m_coefs.at( vex[i] ) += vcf[i];
gsMatrix< T > m_coefs
Coefficient matrix of size coefsSize() x geoDim()
Definition: gsGeometry.h:624
Z binomial(const Z n, const Z r)
Computes the binomial expansion coefficient binomial(n,r)
Definition: gsCombinatorics.h:69
S give(S &x)
Definition: gsMemory.h:266
Provides declaration of Geometry abstract interface.
#define index_t
Definition: gsConfig.h:32
#define GISMO_ASSERT(cond, message)
Definition: gsDebug.h:89
T at(index_t i) const
Returns the i-th element of the vectorization of the matrix.
Definition: gsMatrix.h:211
void stringReplace(std::string &str, const std::string &oldStr, const std::string &newStr)
An univariate polynomial in monomial basis.
Definition: gsMonomialPoly.h:21
#define gsWarn
Definition: gsDebug.h:50
EIGEN_STRONG_INLINE abs_expr< E > abs(const E &u)
Absolute value.
Definition: gsExpressions.h:4486
virtual const gsBasis< T > & basis() const =0
Returns a const reference to the basis of the geometry.
short_t geoDim() const
Dimension n of the absent physical space.
Definition: gsGeometry.h:292
gsMatrix< T > & coefs()
Definition: gsGeometry.h:340