-
Hello, I'm trying to understand the multivariate polynomials. I have a long polynomial with 5 variables I'm able to list the exponents of
Then I know that the coefficient of I could get what I want in this way I think. But isn't there a more direct way? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
Yes, I get what I wanted:
The variables of the right members should be |
Beta Was this translation helpful? Give feedback.
-
Hello,
std::stringstream buffer;
buffer << value << std::endl;
std::string coefab = buffer.str(); But that does not work. I get |
Beta Was this translation helpful? Give feedback.
-
I've reproduced this polynomial expansion that I did with Julia. Found the same results :-) The speed is more or less the same (about one minute, while it takes 20 minutes with Python). Now it remains to use gmp coefficients to preserve the fractions (so far I used double in CGAL). Here is my code. Maybe you will see something that can be improved (I'd like to beat Julia). typedef CGAL::Polynomial_type_generator<double, 9>::Type Poly_9;
typedef CGAL::Polynomial_traits_d<Poly_9> PT_9;
typedef std::pair<CGAL::Exponent_vector, PT_9::Innermost_coefficient_type> Monomial_9;
typedef CGAL::Polynomial_type_generator<double, 6>::Type Poly_6;
typedef CGAL::Polynomial_traits_d<Poly_6> PT_6;
typedef std::tuple<int, int, int> Expo3;
typedef std::map<Expo3, Poly_6> XYZ6;
Rcpp::StringMatrix test4(
Rcpp::IntegerMatrix Powers, Rcpp::NumericVector Coeffs
) {
PT_9::Construct_polynomial construct_polynomial;
int nterms = Coeffs.size();
std::list<Monomial_9> terms;
for(int i = 0; i < nterms; i++) {
Rcpp::IntegerVector powers = Powers(Rcpp::_, i);
terms.push_back(
std::make_pair(
CGAL::Exponent_vector(powers.begin(), powers.end()),
Coeffs(i)
)
);
}
Poly_9 P = construct_polynomial(terms.begin(), terms.end());
std::list<Monomial_9> monoms;
PT_9::Monomial_representation mrepr;
mrepr(P, std::back_inserter(monoms));
XYZ6 Result;
Poly_6 w0 = PT_6::Shift()(Poly_6(1), 1, 0); //
Poly_6 sqrt3 = PT_6::Shift()(Poly_6(1), 1, 1); //
Poly_6 A = PT_6::Shift()(Poly_6(1), 1, 2); //
Poly_6 B = PT_6::Shift()(Poly_6(1), 1, 3); //
Poly_6 C = PT_6::Shift()(Poly_6(1), 1, 4); //
Poly_6 D = PT_6::Shift()(Poly_6(1), 1, 5); //
std::list<Monomial_9>::iterator it_monoms;
for(it_monoms = monoms.begin(); it_monoms != monoms.end(); it_monoms++) {
CGAL::Exponent_vector allPowers = (*it_monoms).first;
double coef = (*it_monoms).second;
Expo3 powersXYZ = {allPowers[0], allPowers[1], allPowers[2]};
Poly_6 PAB =
coef * CGAL::ipower(w0, allPowers[3]) * CGAL::ipower(sqrt3, allPowers[4]) *
CGAL::ipower(A, allPowers[5]) * CGAL::ipower(B, allPowers[6]) *
CGAL::ipower(C, allPowers[7]) * CGAL::ipower(D, allPowers[8]);
if(Result.count(powersXYZ) == 0) {
Result.emplace(powersXYZ, PAB);
} else {
Result.at(powersXYZ) += PAB;
}
}
int nmonoms = Result.size();
Rcpp::StringMatrix POVRay(2, nmonoms);
int i = 0;
for(const auto& [key, value] : Result) {
std::string powers = "xyz(" +
std::to_string(std::get<0>(key)) + ", " +
std::to_string(std::get<1>(key)) + ", " +
std::to_string(std::get<2>(key)) + "): ";
std::stringstream buffer;
CGAL::IO::set_pretty_mode(buffer);
buffer << value << "," << std::endl;
std::string coefab = buffer.str();
Rcpp::StringVector monom = Rcpp::StringVector::create(powers, coefab);
POVRay(Rcpp::_, i++) = monom;
}
return Rcpp::transpose(POVRay);
} |
Beta Was this translation helpful? Give feedback.
-
Finally I don't need Julia nor CGAL: I found a way to get the result within a minute with R (with a package implemented in C++). |
Beta Was this translation helpful? Give feedback.
Finally I don't need Julia nor CGAL: I found a way to get the result within a minute with R (with a package implemented in C++).