<div dir="ltr">Well, I did some more digging. The parser itself is fine. What matters is the hashing order of the symbols. There's one particular sequence that drives the factorization routine off the rails.<br><br>#include <ginac/ginac.h><br>#include <iostream><br>#include <algorithm><br><br>int main()<br>{<br> GiNaC::symbol ar[5];<br> std::sort(std::begin(ar), std::end(ar), [](const auto &a, const auto &b)<br> { return a.gethash() < b.gethash(); });<br> GiNaC::symbol &A = ar[4], &w = ar[0], &K = ar[1], &C = ar[2], &B = ar[3];<br> A.set_name("A");<br> B.set_name("B");<br> C.set_name("C");<br> K.set_name("K");<br> w.set_name("w");<br> std::cout << factor(w*w*w*B*B*A-2*w*w*w*K*K*B*B*A+w*C*C*A+w*w*w*K*K*K*K*B*B*A) << '\n';<br>}<br><br>Interestingly, for the library version before 1.8.0, there were four more permutations that produced weird output:<br><br>// GiNaC::symbol &A = ar[0], &w = ar[1], &K = ar[2], &C = ar[3], &B = ar[4];<br>// GiNaC::symbol &A = ar[1], &w = ar[0], &K = ar[2], &C = ar[3], &B = ar[4];<br>// GiNaC::symbol &A = ar[2], &w = ar[0], &K = ar[1], &C = ar[3], &B = ar[4];<br>// GiNaC::symbol &A = ar[3], &w = ar[0], &K = ar[1], &C = ar[2], &B = ar[4];<br><br>As Richard B. Kreckel <kreckel at <a href="http://in.terlu.de">in.terlu.de</a>> wrote, this is not a bug. So, I apologize for misreporting.<br>But still, the result of factorization looks somewhat counterintuitive.<br><br>With best regards,<br>Ivan<br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Feb 2, 2022 at 12:52 AM Vladimir V. Kisil <<a href="mailto:V.Kisil@leeds.ac.uk">V.Kisil@leeds.ac.uk</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> Hello,<br>
<br>
It seems that the problem is not with the factor method but with the<br>
parser. Take the following modification of your example:<br>
<br>
#include <ginac/ginac.h><br>
#include <iostream><br>
#include <sstream><br>
<br>
int main()<br>
{<br>
std::string input("w^3*B^2*A-2*w^3*K^2*B^2*A+w*C^2*A+w^3*K^4*B^2*A");<br>
while (true)<br>
{<br>
std::ostringstream s;<br>
GiNaC::parser reader;<br>
GiNaC::ex e = reader(input);<br>
<br>
s << factor(e);<br>
if (s.str().length() <= input.length())<br>
continue;<br>
std::cout << s.str() << '\n';<br>
return 0;<br>
}<br>
}<br>
<br>
It behaves as your example and quickly terminates with a strange<br>
output. However, if the declaration "GiNaC::parser reader;" will be<br>
moved out of the loop then the programme will cycle forever.<br>
<br>
Best wishes,<br>
Vladimir<br>
-- <br>
Vladimir V. Kisil <a href="http://www.maths.leeds.ac.uk/~kisilv/" rel="noreferrer" target="_blank">http://www.maths.leeds.ac.uk/~kisilv/</a><br>
Book: Geometry of Mobius Maps <a href="https://doi.org/10.1142/p835" rel="noreferrer" target="_blank">https://doi.org/10.1142/p835</a><br>
Soft: Geometry of cycles <a href="http://moebinv.sourceforge.net/" rel="noreferrer" target="_blank">http://moebinv.sourceforge.net/</a><br>
Jupyter notebooks: <a href="https://github.com/vvkisil?tab=repositories" rel="noreferrer" target="_blank">https://github.com/vvkisil?tab=repositories</a><br>
>>>>> On Mon, 31 Jan 2022 00:01:48 +0300, Ivan Vasilyev <<a href="mailto:grabesstimme@gmail.com" target="_blank">grabesstimme@gmail.com</a>> said:<br>
<br>
IV> Hi! I believe there's an intermittent bug in GiNaC::factor()<br>
IV> function. Sometimes it leaves uncancelled terms with seemingly<br>
IV> random coefficients like:<br>
<br>
IV> w*(w^2*(K^4*B^2+B^2-2*K^2*B^2)+196*w^2*K^4+196*w^2+C^2-196*w^2*(1+K^4-2*K^2)-392*w^2*K^2)*A<br>
<br>
IV> The minimal code to reproduce the bug in GiNaC 1.8.2 is:<br>
<br>
IV> #include <ginac/ginac.h> #include <iostream><br>
<br>
IV> int main() { std::string<br>
IV> input("w^3*B^2*A-2*w^3*K^2*B^2*A+w*C^2*A+w^3*K^4*B^2*A"); while<br>
IV> (true) { std::ostringstream s; GiNaC::parser reader; s <<<br>
IV> factor(reader(input)); if (s.str().length() <= input.length())<br>
IV> continue; std::cout << s.str() << '\n'; return 0; } }<br>
<br>
IV> Also, this problem can be reproduced on<br>
IV> <a href="https://daninet.github.io/ginac-wasm/" rel="noreferrer" target="_blank">https://daninet.github.io/ginac-wasm/</a> with the same expression<br>
IV> being calculated several times in a row:<br>
<br>
IV> factor(w^3*B^2*A-2*w^3*K^2*B^2*A+w*C^2*A+w^3*K^4*B^2*A);<br>
<br>
IV> With best regards, Ivan.</blockquote></div>