# Revision history [back]

The following is a solution based roughly on the algebraic idea, that when dealing with rational functions and the signs of their derivatives we can always do computations so far, that the sign of a polynomial has to be computed. Polynomials are quick enough to compute, and this is the last wall of a quick improvement.

So let us start with the given function $f$, compute $|f|^2$ as in the question and the other answer and write it in the form $$|f|^2 = \frac AB\ .$$

var('x, y')

assume( x, 'real' )
assume( y, 'real' )

f = ( 245*x^4 / (20*x^2 - I*x*(20*y + 1) - 20) - 100*x^2 + I*x + 100 )

ref = real(f)
imf = imag(f)

print ( "...ref and imf have the same denominator? %s"
% bool( ref.denominator() == imf.denominator() ) )

absf2 = ( ref^2 + imf^2 ).factor()

A = absf2.  numerator()
B = absf2.denominator()

print ( "...just a check: Is abs(f)^2 == A/B ? %s"
% bool( ref^2 + imf^2 == A/B ) )


(The check i for my nerves, sometimes a factor like $-2017$ gets lost. A $-1$ is also enough to invert the picture.)

The computed values of $A$ (manually rearranged) and $B$ are:

sage: A
3080025*x^8 + 4000000*x^6*y^2 + 409800*x^6*y - 14029110*x^6
- 7999600*x^4*y^2 - 799960*x^4*y + 22999201*x^4 + 4000000*x^2*y^2
+ 400000*x^2*y - 15989600*x^2 + 4000000

sage: B
400*x^4 + 400*x^2*y^2 + 40*x^2*y - 799*x^2 + 400


Note that the denominator $B$ is $\ge0$, since it is

sage: ( (20*x^2 - I*x*(20*y + 1) - 20) * (20*x^2 + I*x*(20*y + 1) - 20) ).expand()
400*x^4 + 400*x^2*y^2 + 40*x^2*y - 799*x^2 + 400


but we will not need this. (This was just an other check.)

Now we need the differential of $\sqrt{A/B}$. This is $$\left(\ \sqrt{\frac AB}\ \right)' =\frac 1{2\sqrt{\dots}}\frac 1{B^2}(A'B-AB')\ .$$ So we need the sign of the polynomial $$A'B-AB'\ .$$ The code for its plot on the square $[0,2]^2$ is then given by the further lines:

G = ( diff( A, x ) * B - A * diff( B, x ) ).factor()

P = region_plot( G < 0
, (x, 0, 2)
, (y, 0, 2)
, plot_points  = 100
, aspect_ratio = 1 )
P . show()


N.B.

Despite of my effort to control the sign, i've got the colors inverted...

One more sign check:

sage: diff( real(f)^2 + imag(f)^2 , x ).subs(x=0.1).subs(y=0.1)
-3969.59625416928
sage: G.subs(x=0.1).subs(y=0.1)
-6.10388687137070e8