Re: turning differences into sums

Claude Heiland-Allen <[email protected]> Sun, 21 Jun 2026 16:01:07 +0200
Newsgroups gmane.comp.mathematics.maxima.general
Message-ID <[email protected]>
perfect! Thanks Stavros.


here's that trick applied to my problem:


display2d : false;       
                                             
s(x,y,z,w) := (x^2 + y^2 - z^2 - w^2) * (x^2 - y^2) / (x^2 + y^2);    

t(x,y,z,w) := (x^2 + y^2 - z^2 - w^2) * (2 * x * y) / (x^2 + y^2);

u(x,y,z,w) := 2 * sqrt((x^2 + y^2) * (z^2 + w^2)) * (z^2 - w^2) / (z^2 + w^2);

v(x,y,z,w) := 2 * sqrt((x^2 + y^2) * (z^2 + w^2)) * (2 * z * w) / (z^2 + w^2);        
                                                      
p(f) := factor(expand(f(X+x,Y+y,Z+z,W+w) - f(X,Y,Z,W)));
                                                                      p(s);
p(t);
p(u);                                                                 p(v);          
                                                                                                                             m : substpart("-", expand(args(-p(v))[1]), 0);

apply("/", [-expand(args(-p(v))[1] * m), ratsimp(args(-p(v))[2] * m)]);                                                                     



Thanks,


Claude


On 21 June 2026 14:24:54 CEST, Stavros Macrakis <[email protected]> wrote:
>BTW, a trick for getting A+B from A-B is
>
>substpart("-",ex,0)
>
>Why - and not +? Because Maxima treats A-B as A+(-B)
>
>
>On Sun, Jun 21, 2026, 07:39 Barton Willis <[email protected]> wrote:
>
>> If your primary goal is accurate floating-point evaluation, you might try
>> Herbie <https://github.com/herbie-fp/herbie>.  But the online
>> <http://herbie.uwplse.org> interface to Herbie timed out when I tried
>> loading it a moment ago.
>>
>> Let us know what you all discover.
>>
>> --Barton
>>
>>
>>
>>
>>
>>
>> ------------------------------
>> *From:* Claude Heiland-Allen <[email protected]>
>> *Sent:* Sunday, June 21, 2026 6:02 AM
>> *To:* Stavros Macrakis <[email protected]>
>> *Cc:* maxima-discuss <[email protected]>
>> *Subject:* Re: [Maxima-discuss] turning differences into sums
>>
>> Caution: Non-NU Email
>>
>>
>> Thanks for your reply Stavros, however the first example doesn't make
>> any difference in this case and the second one has the copy/paste issue
>> that I'm trying to avoid (though, it uses a nicer syntax than my
>> previous attempts, which is good to learn).
>>
>> Concretely, I want to programmatically transform all the subtractions in
>> the numerator to additions, that is, go from
>>
>> -((4*(W*sqrt(Y^2+X^2)*Z*sqrt(z^2+2*Z*z+w^2+2*W*w+Z^2+W^2)
>>              -sqrt(Z^2+W^2)*w*sqrt(y^2+2*Y*y+x^2+2*X*x+Y^2+X^2)*z
>>              -W*sqrt(Z^2+W^2)*sqrt(y^2+2*Y*y+x^2+2*X*x+Y^2+X^2)*z
>>              -Z*sqrt(Z^2+W^2)*w*sqrt(y^2+2*Y*y+x^2+2*X*x+Y^2+X^2)
>>
>> -W*Z*sqrt(Z^2+W^2)*sqrt(y^2+2*Y*y+x^2+2*X*x+Y^2+X^2)))
>>
>>
>> to
>>
>>
>> -((4*(W*sqrt(Y^2+X^2)*Z*sqrt(z^2+2*Z*z+w^2+2*W*w+Z^2+W^2)
>> +sqrt(Z^2+W^2)*w*sqrt(y^2+2*Y*y+x^2+2*X*x+Y^2+X^2)*z
>>
>> +W*sqrt(Z^2+W^2)*sqrt(y^2+2*Y*y+x^2+2*X*x+Y^2+X^2)*z
>> +Z*sqrt(Z^2+W^2)*w*sqrt(y^2+2*Y*y+x^2+2*X*x+Y^2+X^2)
>> +W*Z*sqrt(Z^2+W^2)*sqrt(y^2+2*Y*y+x^2+2*X*x+Y^2+X^2)))
>>
>>
>> so that I can the multiply the parts of the fraction by it.
>>
>>
>> Thanks,
>>
>>
>> Claude
>>
>> On 21/06/2026 12:49, Stavros Macrakis wrote:
>> > Try
>> >
>> > factor(1/ratsimp(1/ex)),algebraic;
>> >
>> > ratsimp/algebraic generally eliminates roots in the denominator
>> >
>> > or more "manually"
>> >
>> > apply("/",factor(expand(args(ex)*(sqrt(a)+sqrt(b)))))
>> >
>> >
>> > On Sun, Jun 21, 2026, 04:46 Claude Heiland-Allen <[email protected]>
>> > wrote:
>> >
>> >     Hi all,
>> >
>> >     I have something like
>> >
>> >     (sqrt(a) - sqrt(b)) / c
>> >
>> >     and I would like to multiply to and bottom by
>> >
>> >     (sqrt(a) + sqrt(b))
>> >
>> >     and simplify each to get
>> >
>> >     (a - b) / (c * (sqrt(a) + sqrt(b)))
>> >
>> >     of course my actual expressions are much more complicated (and I
>> >     hope my (a-b) will simplify further) and I cannot see how to do it
>> >     programmatically without lots of manual copy and paste with
>> >     potential for errors.
>> >
>> >     actual code:
>> >
>> >     s(x,y,z,w) := (x^2 + y^2 - z^2 - w^2) * (x^2 - y^2) / (x^2 + y^2);
>> >     t(x,y,z,w) := (x^2 + y^2 - z^2 - w^2) * (2 * x * y) / (x^2 + y^2);
>> >     u(x,y,z,w) := 2 * sqrt((x^2 + y^2) * (z^2 + w^2)) * (z^2 - w^2) /
>> >     (z^2 + w^2);
>> >     v(x,y,z,w) := 2 * sqrt((x^2 + y^2) * (z^2 + w^2)) * (2 * z * w) /
>> >     (z^2 + w^2);
>> >     p(f) := factor(expand(f(X+x,Y+y,Z+z,W+w) - f(X,Y,Z,W)));
>> >     p(s);
>> >     p(t);
>> >     p(u);
>> >     p(v);
>> >
>> >     the last two expressions are the ones I want to manipulate as
>> >     described, here is the last one:
>> >
>> >     -((4*(W*sqrt(Y^2+X^2)*Z*sqrt(z^2+2*Z*z+w^2+2*W*w+Z^2+W^2)
>> >     -sqrt(Z^2+W^2)*w*sqrt(y^2+2*Y*y+x^2+2*X*x+Y^2+X^2)*z
>> >     -W*sqrt(Z^2+W^2)*sqrt(y^2+2*Y*y+x^2+2*X*x+Y^2+X^2)*z
>> >     -Z*sqrt(Z^2+W^2)*w*sqrt(y^2+2*Y*y+x^2+2*X*x+Y^2+X^2)
>> >     -W*Z*sqrt(Z^2+W^2)*sqrt(y^2+2*Y*y+x^2+2*X*x+Y^2+X^2)))
>> >      /(sqrt(Z^2+W^2)*sqrt(z^2+2*Z*z+w^2+2*W*w+Z^2+W^2)))
>> >
>> >
>> >     context: I'm trying to apply perturbation techniques to iterations
>> >     of the 4D Hopfbrot fractal, a cousin of the famous 3D Mandelbulb.
>> >     Perturbation techniques popularized in the last years allow
>> >     computationally efficient deep zooming of 2D fractals like the
>> >     Mandelbrot set, and recently have been applied to the Mandelbulb.
>> >     The main idea is to use "small" differences from one "large"
>> >     reference orbit, and the key step is symbolically simplifying so
>> >     there is no catastrophic cancellation when evaluating numerically
>> >     (i.e. naively (X+x)-X will give 0 instead of x when x << X).
>> >
>> >     Thanks,
>> >
>> >
>> >     Claude
>> >
>> >
>> https://urldefense.com/v3/__https://mathr.co.uk__;!!PvXuogZ4sRB2p-tU!HNBMY8w_rKA70J6zDmTJj3M67tGC5xyBsRZ4uBzQTT764MBPm7zFlVo4-1aaHkqqukTjwTljmsTidt0$
>> >
>> >
>> >     _______________________________________________
>> >     Maxima-discuss mailing list
>> >     [email protected]
>> >
>> https://urldefense.com/v3/__https://lists.sourceforge.net/lists/listinfo/maxima-discuss__;!!PvXuogZ4sRB2p-tU!HNBMY8w_rKA70J6zDmTJj3M67tGC5xyBsRZ4uBzQTT764MBPm7zFlVo4-1aaHkqqukTjwTljtm35Guw$
>> >
>>
>>
>> _______________________________________________
>> Maxima-discuss mailing list
>> [email protected]
>>
>> https://urldefense.com/v3/__https://lists.sourceforge.net/lists/listinfo/maxima-discuss__;!!PvXuogZ4sRB2p-tU!HNBMY8w_rKA70J6zDmTJj3M67tGC5xyBsRZ4uBzQTT764MBPm7zFlVo4-1aaHkqqukTjwTljtm35Guw$
>>

https://mathr.co.uk