1
$\begingroup$

Edit

When I change the x range (e.g. {x,1,3}) I get the same error also for the direct expression. Thus, the problem exists either way and I assume it just evaluated different points?

Problem

I want to create a region plot of the following expression over lambda and x. When I use the expression as an argument it works great, but when I save the expression in a function, I get errors:

RegionPlot[ Log[Re[Cos[1/2 \[Pi] x Sqrt[1 - \[Lambda]]] Cos[ 1/2 \[Pi] x Sqrt[1 + \[Lambda]]] - ( Sin[1/2 \[Pi] x Sqrt[1 - \[Lambda]]] Sin[ 1/2 \[Pi] x Sqrt[1 + \[Lambda]]])/Sqrt[1 - \[Lambda]^2] + 1/2 \[Sqrt](1/(-1 + \[Lambda]^2) (2 - 3 \[Lambda]^2 + \[Lambda]^2 Cos[\[Pi] x Sqrt[ 1 + \[Lambda]]] + Cos[\[Pi] x Sqrt[ 1 - \[Lambda]]] (\[Lambda]^2 + (-2 + \[Lambda]^2) Cos[\ \[Pi] x Sqrt[1 + \[Lambda]]]) + 2 Sqrt[1 - \[Lambda]^2] Sin[\[Pi] x Sqrt[1 - \[Lambda]]] Sin[\[Pi] x Sqrt[ 1 + \[Lambda]]]))]] < 0, {\[Lambda], 0, 0.8}, {x, 0, 17.5}] 

This worked, but the following gives me errors.

staba[\[Lambda]_, x_] := Log[Re[Cos[1/2 \[Pi] x Sqrt[1 - \[Lambda]]] Cos[ 1/2 \[Pi] x Sqrt[1 + \[Lambda]]] - ( Sin[1/2 \[Pi] x Sqrt[1 - \[Lambda]]] Sin[ 1/2 \[Pi] x Sqrt[1 + \[Lambda]]])/Sqrt[1 - \[Lambda]^2] + 1/2 \[Sqrt](1/(-1 + \[Lambda]^2) (2 - 3 \[Lambda]^2 + \[Lambda]^2 Cos[\[Pi] x Sqrt[ 1 + \[Lambda]]] + Cos[\[Pi] x Sqrt[ 1 - \[Lambda]]] (\[Lambda]^2 + (-2 + \[Lambda]^2) Cos[\ \[Pi] x Sqrt[1 + \[Lambda]]]) + 2 Sqrt[1 - \[Lambda]^2] Sin[\[Pi] x Sqrt[1 - \[Lambda]]] Sin[\[Pi] x Sqrt[ 1 + \[Lambda]]]))]] RegionPlot[staba[\[Lambda], x] < 0, {\[Lambda], 0, 0.8}, {x, 0, 17.5}] 

It still gives me the same plot, but I get the errors

Less::nord: Invalid comparison with -1.01093+3.14159 I attempted. Less::nord: Invalid comparison with -1.00272+3.14159 I attempted. Less::nord: Invalid comparison with -1.01196+3.14159 I attempted. General::stop: Further output of Less::nord will be suppressed during this calculation. 

Solution Attempt

It seems like it uses pi once as an analytical constant and once as a number. This would result in Euler's identity not being evaluated in the second attempt. I tried with Evaluate or changing the delayed evaluation (":=" vs. "="). But this did not help. I also get the same problem using the max function instead of the region plot (see below). I could not find anything online, so I'd be glad for explanations and solutions.

Similar Problem & Context

The code is to reproduce Fig. 1.6 from Classical and Quantum Parametric Phenomena. The full code is:

$Assumptions = x \[Element] Reals; w[t_, \[Lambda]_, \[Omega]0_] := {{Cos[\[Omega]0 Sqrt[ 1 - \[Lambda]] t], Sin[\[Omega]0 Sqrt[1 - \[Lambda]] t]}, {-\[Omega]0 Sqrt[ 1 - \[Lambda]] Sin[\[Omega]0 Sqrt[ 1 - \[Lambda]] t], \[Omega]0 Sqrt[ 1 - \[Lambda]] Cos[\[Omega]0 Sqrt[1 - \[Lambda]] t]}}; w[t_, \[Lambda]_] = ( { {Cos[\[Omega]0 Sqrt[1 - \[Lambda]] t], Sin[\[Omega]0 Sqrt[1 - \[Lambda]] t]}, {-\[Omega]0 Sqrt[1 - \[Lambda]] Sin[\[Omega]0 Sqrt[1 - \[Lambda]] t], \[Omega]0 Sqrt[ 1 - \[Lambda]] Cos[\[Omega]0 Sqrt[1 - \[Lambda]] t]} } ); \[CapitalPhi][Tp, 0][\[Lambda]_] = w[Tp, -\[Lambda]] . Inverse[w[Tp/2, -\[Lambda]]] . w[Tp/2, \[Lambda]] . Inverse[w[0, \[Lambda]]]; ca[\[Lambda]_] = Tr[\[CapitalPhi][Tp, 0][\[Lambda]]]/2 + Sqrt[(Tr[\[CapitalPhi][Tp, 0][\[Lambda]]]/2)^2 - 1]; cb[\[Lambda]_] = Tr[\[CapitalPhi][Tp, 0][\[Lambda]]]/2 - Sqrt[(Tr[\[CapitalPhi][Tp, 0][\[Lambda]]]/2)^2 - 1]; \[Mu]a[\[Lambda]_, \[CapitalGamma]_] = Log[Re[ca[\[Lambda]]]]/Tp - \[CapitalGamma]/2; \[Mu]b[\[Lambda]_, \[CapitalGamma]_] = Log[Re[cb[\[Lambda]]]]/Tp - \[CapitalGamma]/2; stability[\[Lambda]_, \[CapitalGamma]_, x_] = Max[Tp \[Mu]a[\[Lambda], \[CapitalGamma]] /. {Tp -> ( x \[Pi])/\[Omega]0}, Tp \[Mu]b[\[Lambda], \[CapitalGamma]] /. {Tp -> ( x \[Pi])/\[Omega]0}]; RegionPlot[ stability[\[Lambda], 0, x] > 0, {\[Lambda], 0, 0.8}, {x, 0, 17.5}, PlotPoints -> 10] (* PC too slow *) 

Here I get the errors again also for the max function

Max::nord: Invalid comparison with -0.458641+3.14159 I attempted. Max::nord: Invalid comparison with 0.458641 +3.14159 I attempted. Greater::nord2: Comparison of Max[-0.458641+3.14159 I,0.458641 +3.14159 I] and 0 is invalid. Max::nord: Invalid comparison with -0.542381+3.14159 I attempted. General::stop: Further output of Max::nord will be suppressed during this calculation. Greater::nord2: Comparison of Max[-0.542381+3.14159 I,0.542381 +3.14159 I] and 0 is invalid. Greater::nord: Invalid comparison with -0.15055+3.14159 I attempted. Greater::nord2: Comparison of Max[-0.624644+3.14159 I,0.624644 +3.14159 I] and 0 is invalid. General::stop: Further output of Greater::nord2 will be suppressed during this calculation. Greater::nord: Invalid comparison with -0.286704+3.14159 I attempted. Greater::nord: Invalid comparison with -0.527562+3.14159 I attempted. General::stop: Further output of Greater::nord will be suppressed during this calculation. 

If I use the expression directly it doesn't produce any errors again.

$\endgroup$
3
  • 2
    $\begingroup$ (1) You get back the same plot, so practically speaking, there is no problem. (2) This issue has nothing to do with the Euler's identity. When you give the expression directly to RegionPlot, it can analyze it and determine its domain. When you give it as an external function, it will only numerically plug in the values, hence it will encounter an error at some points. (3) This issue is commonly solved by either RegionPlot[Evaluate[staba[λ, x] < 0], {λ, 0, 0.8}, {x, 0, 17.5}] or RegionPlot[staba[λ, x] < 0, {λ, 0, 0.8}, {x, 0, 17.5}, Evaluated -> True]. $\endgroup$ Commented May 20, 2024 at 15:04
  • $\begingroup$ I just used it with a different x range ({x,1,3} and then I get the error for both expressions, so it's actually not different for the two ways. I assume different points are evaluated? The Evaluated->True or Evaluate[...] does not fix it. $\endgroup$ Commented May 20, 2024 at 15:09
  • $\begingroup$ The nord errors come from a negative argument to Log that is not internally Quiet-ed. Try On[Log]; staba[0.5, 1.]; Off[Log]or On[Log]; stability[0.5, 0, 1.]; Off[Log]. (And $Assumptions affects only functions that use it, and there are none such in your codes.) In the last code, \[Omega]0 is undefined in stability. $\endgroup$ Commented May 20, 2024 at 16:30

1 Answer 1

1
$\begingroup$

The main problem, whether or not a message is generated when an invalid comparison is made, hinges (or seems to hinge) on whether or not the predicate of RegionPlot can be compiled (without calls to MainEvaluate).

For instance, preventing compilation in the working example yields messages:

RegionPlot[Log[Re[ Cos[1/2 π x Sqrt[1 - λ]] Cos[ 1/2 π x Sqrt[1 + λ]] - (Sin[ 1/2 π x Sqrt[1 - λ]] Sin[ 1/2 π x Sqrt[1 + λ]])/Sqrt[1 - λ^2] + 1/2 Sqrt[ (1/(-1 + λ^2) (2 - 3 λ^2 + λ^2 Cos[π x Sqrt[1 + λ]] + Cos[π x Sqrt[1 - λ]] (λ^2 + (-2 + λ^2) Cos[π x Sqrt[1 + λ]]) + 2 Sqrt[1 - λ^2] Sin[π x Sqrt[1 - λ]] Sin[π x Sqrt[1 + λ]])) ]]] < 0 , {λ, 0, 0.8}, {x, 0, 17.5}, Compiled -> False] 

Less::nord: Invalid comparison with -1.01093+3.14159 I attempted.
Less::nord: Invalid comparison with -1.00272+3.14159 I attempted.
Less::nord: Invalid comparison with -1.01196+3.14159 I attempted.
General::stop: Further output of Less::nord will be suppressed during this calculation.

BEWARE: You cannot force compilation of an uncompilable function. The compilation is not performed by Compile and call-backs to the main kernel via MainEvaluate are not possible. Using Compile -> True on such predicates usually results in the kernel crashing for me.

In the second example, the predicate in RegionPlot[staba[\[Lambda], x] < 0,...] contains an uncompilable function staba. If you replace the predicate by its evaluation, then it becomes compilable. Hence RegionPlot[Evaluate[staba[\[Lambda], x] < 0],...] emits no messages, but RegionPlot[staba[\[Lambda], x] < 0,...] does.

In the last example, stability[] contains an undefined symbol ω0, which makes both stability[\[Lambda], 0, x] > 0 and its value uncompilable. So neither expression is message-free. However, if we set a value for ω0, then the evaluated stability[\[Lambda], 0, x] > 0 is compilable and we get no errors:

Block[{\[Omega]0 = 1}, RegionPlot[ stability[\[Lambda], 0, x] > 0 // Evaluate, {\[Lambda], 0, 0.8}, {x, 0, 17.5}] ] 

< no errors >

I do not know the internals of how the autocompilation of the predicate is done. In the compiled case, an internal Trace reveals that Message[Less::nord,...] is generated multiple times, but that the value of $Messages has been cleared so that there is no output.

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.