# TimeConstrained[] not taking full amount of time

I am using Mathematica to evaluate an integral within a given time constraint. Note that I have read in values of 'BigList' or BL from a file. If the file has a value for (l,m) of zero, then I evaluate the integral, otherwise, I read in the value of the integral from the file. The idea is to continually allow more and more time and 'knock out' the easiest integrals each time, instead of having the script run for hours before stopping when it would get to 'easier' integrals later.

My problem is that, for the integrals which evaluate to zero, the output happens far too quickly. The integrals will ONLY evaluate to zero if the time constraint is met, these integrals will never be zero on their own, so I know that if I get an output of zero, that it did not evaluate the integral in the time constraint allowed.

The output, however, prints (l) (m) 0 (where (l) and (m) are actual numerical values of l and m) almost within less than a second of each other. This tells me that it is not spending the full time (15 seconds in the example below) on the integral. Does Mathematica somehow know that it will take longer and give up or is something else going on? Shouldn't it work for 15 seconds before stopping and resulting in zero?

29 (* total time for each integral allowed *)
30 tt = 15;
31 (* loop through NeedList and assign values *)
32 (* Print["BLsize = ", BLsize]; *)
33 For[i=1, i<=BLsize, i++,
34   If[BL[[i]][[3]]==0,
35     l=BL[[i]][[1]];
36     m=BL[[i]][[2]];
37     (* Assign Jm to be Integrate[Abs[Sin[m*theta],{theta,0,2*pi}]] *)
38     If[m==0,Jm=2*Pi,Jm=4];
39     Ilm=TimeConstrained[Integrate[Abs[LegendreP[l,Abs[m],z]],{z,-1,1}],tt,0];
40     If[Ilm==0, BL[[i]]={l,m,0},
41       If[l==0, BL[[i]]={l,m,1/(Ilm*Jm)}, BL[[i]]={l,m,2/(Ilm*Jm)}];
42     ];
43     (*Print[FortranForm[N[BL[[i]][[3]]]]];*)
44     Print[BL[[i]][[1]], " ", BL[[i]][[2]], " ", FortranForm[N[BL[[i]][[3]]]]];
45   ]
46 ];


EDIT: here is a full working example ...

finp = OpenRead["dens_coefs"];
(* BigList *)
Close[finp];
BLsize = Length[BL];

(* total time for each integral allowed *)
tt = 20;
(* loop through NeedList and assign values *)
For[i=1, i<=BLsize, i++,
If[BL[[i]][[3]]==0,
l=BL[[i]][[1]];
m=BL[[i]][[2]];
(* Assign Jm to be Integrate[Abs[Sin[m*theta],{theta,0,2*pi}]] *)
If[m==0,Jm=2*Pi,Jm=4];
Ilm=TimeConstrained[Integrate[Abs[LegendreP[l,Abs[m],z]],{z,-1,1}],tt,0];
If[Ilm==0, BL[[i]]={l,m,0},
If[l==0, BL[[i]]={l,m,1/(Ilm*Jm)}, BL[[i]]={l,m,2/(Ilm*Jm)}];
];
Print[BL[[i]][[1]], " ", BL[[i]][[2]], " ", FortranForm[N[BL[[i]][[3]]]]];
]
];


an example of an input file might be:

3 -3 0.02829421210522584
3 -2 0.06666666666666667
3 -1 0.5706901657772195
3 0 0.
3 1 0.5706901657772195
3 2 0.06666666666666667
3 3 0.02829421210522584
4 -4 0.004464285714285714
4 -3 0.011904761904761904
4 -2 0.1361783763047951
4 -1 0.
4 0 0.
4 1 0.
4 2 0.1361783763047951
4 3 0.011904761904761904
4 4 0.004464285714285714


the program should read these numbers in, figure out which ones are zero (in this case, {3,0}, {4,-1}, {4,0}, {4,1}) and attempt to produce the corresponding integrals. it should take no more than 20 seconds, but if it returns 'zero' then it should take AT LEAST 20 seconds (very close to 20 seconds, maybe a few clock cycles over).

for what it is worth, i am running this from a command line script on a linux machine. i have tried TimeConstrained arguments in a Mathematica notebook (though not the exact same thing) and it waits, as it should, in the notebook, but it is not waiting the full 20 seconds in the script.

EDIT(2): for what it's worth, this seems to work as intended when running it from a notebook interface. it is only when running the script from command line that it gives me problems. any idea why that would happen?

-

## migrated from stackoverflow.comJul 14 '12 at 0:29

This question came from our site for professional and enthusiast programmers.

Could you post a complete working minimal example (without the need to imagine what is in your file)? Also, please remove the line numbers, so easing the copy and paste of your code into Mma. –  belisarius Jul 1 '12 at 19:43
I'm not really following your application. Simply stated do you want TimeConstrained to pause for until 20 seconds have elapsed, even if the calculation only takes e.g. one second? –  Mr.Wizard Jul 5 '12 at 13:15
no, the problem is that it returns '0' right away on some cases. the integral should never evaluate to zero until 20 seconds of computation have been done because '0' is the return value upon failure of TimeConstrained. (failure is relative - it returns zero if it takes the full amount of time). obviously, it is a problem if it returns zero in less than half a second when i have alotted 20 seconds for the calculation. –  Laurbert515 Jul 5 '12 at 14:48
Despite your lengthy post I'm afraid I just don't understand the question; the default return value upon failure is $Aborted, not 0. Why would you specify it to be 0 if that conflicts with the other parameters of your program? Why don't you simply test for $Aborted if you want to know if a calculation was, well, aborted? Surely there is some aspect of this I don't understand; I'd genuinely like to be helpful if I can be made to understand it. –  Mr.Wizard Jul 9 '12 at 0:26
i have the output set to zero if it does not work because i have this writing to a file. these calculations take a long time, so the 'first' run of the program, i allow, say 20 seconds. the ones that were able to be calculated in that time are written as they are, the others are zero. then i run it again with a time alotment of, say, a minute. this time, however, i only calculate the values which are read as 'zero' in the file, because i know the others are already calculated correctly. –  Laurbert515 Jul 9 '12 at 13:26

TimeConstrained states:

TimeConstrained [expr, t, failexpr ]
returns failexpr if the time constraint is not met.


So your assumption that it MUST calculate for 20 is, i'm sorry to say, wrong.

TimeConstrained generates an interrupt to abort the evaluation of expr if the evaluation is not completed within the specified time.

So, if Mathematica can know that the answer is 0 before the 20 seconds are up then it is correct.

TimeConstrained will interrupt your calculation if it takes longer than 20 seconds only.

Also, TimeConstrained evaluates failexpr only if the evaluation is aborted.

EDIT:

After looking through the code more thoroughly, I have come to the conclusion that the reason you might be seeing 0 so quickly might be be cause of the orthogonality of the polynomials being generated. When L + m are odd, the integral evaluates to 0. This is because the integrand, ie the resulting Legendre Polynomial, is an odd function. See equation 6 in the paper:

For more detailed info, see The Integral of the Associated Legendre Function

-
that is the problem, the answer is NOT zero. i know that for a fact, which is why zero is the failed expression. it should only return zero if it could not find the true answer within the allotted time. Now, if Mathematica could somehow know that it would not be able to produce the answer within the allotted time and aborts without even trying, that is another issue - but it does not seem to be what you are saying happens. –  Laurbert515 Jul 11 '12 at 21:21
I have edited my previous answer. I do believe that when you integrate, sometimes the result WILL be 0 before the time expires. If this is not the behavior you are seeing, can you supply the input and output that you receive? This will allow me to see if you are sometimes integrating regular Legendre polynomials with order 0, or if they are associated Legendre polynomials with degree and order >0. –  sisharp Jul 12 '12 at 14:22
i am not integrating multiples of legendre polynomials. only Abs[LegendreP[l,Abs[m],z]] which is only ONE associated legendre polynomial, not multiple. the integral will never be zero. –  Laurbert515 Jul 13 '12 at 5:15
I never mentioned multiple polynomials. If you look at the white paper, you will see it is exactly what the function does. In addition, you did not see that the polynomial that gets generated by Legendrep can sometimes be odd. And, if you remember way back from pre calculus, integrating an odd function that is continuous within an interval, will ALWAYS be 0. Again, see the paper The Integral of the Associated Legendre Function. The integral will be sometimes be 0. –  sisharp Jul 13 '12 at 12:53
i am taking the absolute value of the odd legendre polynomial, so no, it will not be zero. –  Laurbert515 Jul 13 '12 at 14:51