16 #include <boost/lexical_cast.hpp>
17 #include <boost/algorithm/string.hpp>
43 std::runtime_error(msg),
mSubstring(std::move(substring))
50 std::stringstream strstr;
51 strstr << std::runtime_error::what() <<
" at " <<
mSubstring <<
" in " << inputString;
75 std::map<std::string, Variable>
mVars;
86 const std::map<std::string, Variable>&
variables()
const {
127 CARL_LOG_ASSERT(
"carl.stringparser", to,
"Extended parser not supported");
134 std::vector<std::string> nomAndDenom;
135 boost::split(nomAndDenom, inputString, boost::is_any_of(
"/"));
136 assert(!nomAndDenom.empty());
137 if(nomAndDenom.size() > 2)
141 else if(nomAndDenom.size() == 2)
143 auto nom = parseMultivariatePolynomial<C>(nomAndDenom.front());
144 auto denom = parseMultivariatePolynomial<C>(nomAndDenom.back());
153 assert(nomAndDenom.size() == 1);
154 auto pol = parseMultivariatePolynomial<C>(nomAndDenom.front());
163 std::vector<std::string> termStrings;
166 boost::split(termStrings,inputString,boost::is_any_of(
"+"));
168 for(std::string& tStr : termStrings)
173 result += parseTerm<C>(tStr);
193 std::vector<std::pair<Variable, exponent>> varExpPairs;
196 std::vector<std::string> varExpPairStrings;
197 boost::split(varExpPairStrings, inputStr, boost::is_any_of(
"*"));
200 for(
const std::string& veStr : varExpPairStrings)
202 std::vector<std::string> varAndExp;
203 boost::split(varAndExp, veStr, boost::is_any_of(
"^"));
204 if(varAndExp.size() > 2)
209 if(varAndExp.size() == 1)
211 auto it =
mVars.find(veStr);
212 if(it !=
mVars.end())
214 varExpPairs.emplace_back(it->second, 1);
218 coeff *= constructCoefficient<C>(veStr);
223 assert(varAndExp.size() == 2);
224 auto it =
mVars.find(varAndExp.front());
225 if(it !=
mVars.end())
229 unsigned exp = boost::lexical_cast<unsigned>(varAndExp.back());
230 varExpPairs.emplace_back(it->second,
exp);
232 catch(
const boost::bad_lexical_cast&)
249 std::sort(varExpPairs.begin(), varExpPairs.end(), [](
const std::pair<Variable, exponent>& p1,
const std::pair<Variable, exponent>& p2){ return p1.first < p2.first; });
250 size_t nrVariables = varExpPairs.size();
251 std::unique(varExpPairs.begin(), varExpPairs.end());
252 if(nrVariables != varExpPairs.size())
256 if(varExpPairs.empty())
262 std::shared_ptr<const Monomial> result =
createMonomial( std::move(varExpPairs) );
272 std::string inputStringCopy = inputString;
273 assert(!inputStringCopy.empty());
274 if( inputStringCopy[0] ==
'(' )
276 assert(inputStringCopy.back() ==
')');
277 inputStringCopy = inputStringCopy.substr( 1, inputStringCopy.size()-2 );
281 return parse<C>(inputStringCopy);
283 catch(std::exception&)
A small wrapper that configures logging for carl.
#define CARL_LOG_ASSERT(channel, condition, msg)
#define CARL_LOG_NOTIMPLEMENTED()
Monomial::Arg createMonomial(T &&... t)
Interval< Number > exp(const Interval< Number > &i)
Variable fresh_real_variable() noexcept
The general-purpose multivariate polynomial class.
The default policy for polynomials.
Represents a single term, that is a numeric coefficient and a monomial.
std::string mErrorString
Error message.
virtual cstring what() const noexcept override
const std::string mSubstring
Substring where the problem is.
InvalidInputStringException(const std::string &msg, std::string substring, const std::string &inputString="")
void setInputString(const std::string &inputString)
bool mSingleSymbVariables
std::map< std::string, Variable > mVars
void setVariables(std::list< std::string > variables)
void setSumOfTermsForm(bool to)
In SumOfTermsForm, input strings are expected to be of the form "c_1 * m_1 + ... + c_n * m_n",...
const std::map< std::string, Variable > & variables() const
MultivariatePolynomial< C, O, P > parseMultivariatePolynomial(const std::string &inputString) const
C constructCoefficient(const std::string &inputString) const
bool setImplicitMultiplicationMode(bool to)
Term< C > parseTerm(const std::string &inputStr) const
bool mImplicitMultiplicationMode
RationalFunction< MultivariatePolynomial< C, O, P > > parseRationalFunction(const std::string &inputString) const