0

First of all, I am not entirely sure what the problem is. The code I am presenting worked in a previous version of Mathematica but not now in Version 11.0.1. Essentially, I am calculating Point Spread Functions (PSF) according to the theory outlined by Born & Wolf.

 {λIll = 0.488, nIll = 1.518, NAIll = 1.3};
    optIll = {v -> 2 π n r Sin[α]/λ, 
   u -> 2 π n z Sin[α]^2/λ, α -> 
    ArcSin[NA/n], n -> nIll, λ -> λIll, 
   NA -> NAIll, ρ -> rho};

The integral is split into two parts. The factor bwFIll and the integrand bwIIll are simplified separately.

bwFIll = Function[{r, z}, 
  Evaluate@FullSimplify[-2.0 π I n Sin[α]^2/λ Exp[
       I u/Sin[α]^2] //. optIll, ass]]

Output

(* Function[{r, z}, (0. - 14.3343 I) E^((0. + 19.5448 I) z)] *)

Texte

bwIIll = Function[{r, z}, 
  Evaluate@FullSimplify[
    BesselJ[0, v ρ] Exp[-0.5 I u ρ^2] ρ //. optIll, 
    ass]]

Output

(*  Function[{r, z}, E^((0. - 7.16713 I) rho^2 z) rho BesselJ[0, 16.738 r rho]] *)

What drives me nuts is the presentation of the complex number as 0.0 - 7.16713 I instead of as simply as -7.16713 I. This was different in my original Notebook. I cannot reproduce this but I still have the notebook.

bwFullIll[r_, z_] := 
 bwFIll[r, z] NIntegrate[bwIIll[r, z], {rho, 0.0, 1.0}, 
   AccuracyGoal -> 6, WorkingPrecision -> MachinePrecision]

Calling the function is fine.

bwFullIll0 = bwFullIll[0, 0]

Output

(* 0. - 7.16713 I  *)

This function calculates a scalar PSF.

illFull[r_, z_] := 
 Block[{uill}, uill = bwFullIll[r, z]/bwFullIll0; 
  Chop[uill Conjugate[uill]]]

However, this function FindRoot and other functions such as NIntegrate all produce the same error list.

illR0 = (x /. FindRoot[illFull[x, 0.0] == 0.0, {x, 0.22}])

Output

During evaluation of In[9]:= NIntegrate::inumr: The integrand (1. +0. I) rho BesselJ[0,16.738 rho x] has evaluated to non-numerical values for all sampling points in the region with boundaries {{0.,1.}}.

During evaluation of In[9]:= NIntegrate::inumr: The integrand (1. +0. I) rho BesselJ[0,16.738 rho x] has evaluated to non-numerical values for all sampling points in the region with boundaries {{0.,1.}}.

During evaluation of In[9]:= NIntegrate::inumr: The integrand (1. +0. I) rho BesselJ[0,16.738 rho x] has evaluated to non-numerical values for all sampling points in the region with boundaries {{0.,1.}}.

During evaluation of In[9]:= General::stop: Further output of NIntegrate::inumr will be suppressed during this calculation.

Out[9]= 0.228923

I have no clue what is going on and really need to know what is causing the error message. I should add that I experienced with various options AccuracyGoal -> 6, WorkingPrecision -> MachinePrecision but to no avail. What works is the use of Table.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Ernst Stelzer
  • 2,055
  • 12
  • 24
  • 2
    Define illFull[r_?NumericQ, z_?NumericQ] ... See http://mathematica.stackexchange.com/questions/18393/what-are-the-most-common-pitfalls-awaiting-new-users/26037#26037 -- As for the display of complex numbers, that seems to be how it's done (and has been for a while, I think). Reformatting output is possible, probably, but maybe not easy. – Michael E2 Jan 17 '17 at 19:00
  • Thank you very much. This still leaves the issue with the way the (non-)complex numbers are presented. – Ernst Stelzer Jan 17 '17 at 19:24
  • The annoying issue is that Chop has no effect. The 0. is shown independently of whether the real or the imaginary part are zero. I also had another look at an older notebook (2001, no idea which version I used then). The r_?NumericQ, which I have been using in other circumstances, was not required and the complex values reduced to the expected simpler values or are at least shown in simpler terms. – Ernst Stelzer Jan 18 '17 at 08:12
  • Running bwFullIll0 = bwFullIll[0, 0] // Chop returns 0. - 7.16713 I. I.e. no effect of Chop. – Ernst Stelzer Jan 18 '17 at 13:02

2 Answers2

2

The code I am presenting worked in a previous version of Mathematica but not now in Version 11.0.1.

What drives me nuts is the presentation of the complex number as 0.0 - 7.16713 I instead of as simply as -7.16713 I. This was different in my original Notebook.

I also had another look at an older notebook (2001, no idea which version I used then).

In Mathematica version 8 was a change in how complex numbers are represented internally and displayed within the Notebook interface. Originally the representation was as you describe:

$Version
2. I
% // FullForm
Developer`PackedArrayQ@Developer`ToPackedArray[{1. + I, 1.1 I}]
"5.2 for Microsoft Windows x86 (64 bit) (June 20, 2005)"
2. I
Complex[0, 2.`]
False

But now it is different:

$Version
2. I
% // FullForm
Developer`PackedArrayQ@Developer`ToPackedArray[{1. + I, 1.1 I}]
"11.1.0 for Microsoft Windows (64-bit) (March 13, 2017)"
0. + 2. I    
Complex[0.`, 2.`]
True

This was an intentional change in order to allow packed arrays of complex numbers (according to a discussion on MathGroup long time ago). In a related discussion an example of a code broken due to this change was given (I simplify it even further here):

fnc = Interpolation[{{0, 0}, {2, 2}}, InterpolationOrder -> 1];
fnc[(1. I)/I]

InterpolatingFunction[{{0, 2}}, <>][1. + 0. I]

(in versions before version 8 the output was 1.). As a workaround it was suggested to use Chop:

fnc[Chop[(1. I)/I]]
1.

But in your particular case the source of the problem is different: it is just preprocessing introduced in FindRoot which can be disabled using undocumented option Evaluated -> False:

illR0 = (x /. FindRoot[illFull[x, 0.0] == 0.0, {x, 0.22}, Evaluated -> False])
0.228921

See the following answers for more information:

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
0

You might find this useful in displaying complex numbers (no guarantees you won't find cases where this leads to incorrect results):

cleanComplexForm[expr_] := 
 expr /. Complex[r_, i_] /; PossibleZeroQ[r] :> i Defer[I]


E^((0.-7.16713 I) rho^2 z) rho BesselJ[0,16.738 r rho]//cleanComplexForm
(* E^(-7.16713 rho^2 z I) rho BesselJ[0,16.738 r rho] *)
chuy
  • 11,205
  • 28
  • 48