本文整理汇总了C++中ginac::ex::nops方法的典型用法代码示例。如果您正苦于以下问题:C++ ex::nops方法的具体用法?C++ ex::nops怎么用?C++ ex::nops使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ginac::ex
的用法示例。
在下文中一共展示了ex::nops方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: print_power
// treat powers specially, because it's better to unroll them and
// avoid a function call if we have an exact integral power
std::string cpp_cse::print_power(const GiNaC::ex& expr, bool use_count)
{
std::ostringstream out;
size_t n = expr.nops();
if(n != 2)
{
error_context err_ctx = this->data_payload.make_error_context();
err_ctx.error(ERROR_CSE_POWER_ARGUMENTS);
out << "std::pow(" << this->print_operands(expr, ",", use_count) << ")";
return std::string{};
}
// set up aliases for base and exponent expressions
const auto& base_expr = expr.op(0);
const auto& exponent_expr = expr.op(1);
// perform use-counting on exponent, which is necessary since its values may have been
// used elsewhere in the CSE tree, even if it is an integer that we will unroll and not use explicitly
std::string exponent;
if(use_count) exponent = this->get_symbol_with_use_count(exponent_expr);
else exponent = this->get_symbol_without_use_count(exponent_expr);
if(GiNaC::is_a<GiNaC::numeric>(exponent_expr))
{
const auto& exp_numeric = GiNaC::ex_to<GiNaC::numeric>(exponent_expr);
std::string base;
if(use_count) base = this->get_symbol_with_use_count(expr.op(0));
else base = this->get_symbol_without_use_count(expr.op(0));
if(GiNaC::is_integer(exp_numeric))
{
if(GiNaC::is_nonneg_integer(exp_numeric))
{
if(exp_numeric.to_int() == 0) out << "1.0";
else if(exp_numeric.to_int() == 1) out << unwrap_power(base, base_expr, 1);
else if(exp_numeric.to_int() == 2) out << "(" << unwrap_power(base, base_expr, 2) << ")";
else if(exp_numeric.to_int() == 3) out << "(" << unwrap_power(base, base_expr, 3) << ")";
else if(exp_numeric.to_int() == 4) out << "(" << unwrap_power(base, base_expr, 4) << ")";
else out << "std::pow(" << base << "," << exp_numeric.to_int() << ")";
}
else // negative integer
{
if(exp_numeric.to_int() == -0) out << "1.0";
else if(exp_numeric.to_int() == -1) out << "1.0/" << unwrap_power(base, base_expr, 1);
else if(exp_numeric.to_int() == -2) out << "1.0/" << "(" << unwrap_power(base, base_expr, 2) << ")";
else if(exp_numeric.to_int() == -3) out << "1.0/" << "(" << unwrap_power(base, base_expr, 3) << ")";
else if(exp_numeric.to_int() == -4) out << "1.0/" << "(" << unwrap_power(base, base_expr, 4) << ")";
else out << "std::pow(" << base << "," << exp_numeric.to_int() << ")";
}
}
else // not an integer
{
out << "std::pow(" << base << "," << exponent << ")";
}
}
return(out.str());
}