Re: Lazy expression evaluation

From: David Brown <dbrown_at_nyahnyahspammersnyahnyah>
Date: Tue, 16 Jun 2009 18:57:58 -0600

On Jun 16, 2009, at 6:30 PM, Dave Allured wrote:

> Dave B,
>
> Thanks for looking at this. I think that lazy expression evaluation
> for array expressions would be beneficial.
>
> It seems to me that that lazy evaluation was never implemented in
> NCL just for "if" statements. Lazy evaluation also works in general
> scalar expressions, just not in array expressions. In this example
> for current NCL versions, check1 is a scalar expression assignment
> which could not have the indicated result without lazy evaluation:
>

Yes, I was not completely clear. I meant that that I think the
motivation for lazy evaluation was "if" statements, and, as a side
effect, it came to be a feature of scalar logical expressions.
Otherwise it could have been implemented for both scalar and array
logical expressions, without distinguishing between them, in the
same part of the code, and, with the benefit of hindsight, I think
more simply. But it's also quite possible (ok, probable) that it was
intended for array logical expressions from the beginning. In fact,
except for the complication of missing values, array logical
expressions do provide lazy evaluation since they ultimately use the
C '&&' operation for all non-missing values. Of course I am just
speculating about the thought process of the original developer.
Maybe I will ask him sometime.
  -dave

> a = (/ 1,2,3 /)
> a@_FillValue = 2
> check1 = (.not.ismissing(a(1)) .and. (a(1) .gt. 0))
> check2 = (.not.ismissing(a) .and. (a .gt. 0))
>
> print (check1)
> print (check2(1))
>
> (0) False
> (0) Missing
>
> Also I see in the same documentation under "if statements", there is
> an almost explicit reference to using the ismissing function in
> array mode with lazy expression evaluation. "The function ismissing
> returns an array ..." "Combined with lazy conditional expression
> evaluation..." There are other suggestive statements in the same
> section. This seems like simply an incomplete implementation of
> lazy evaluation.
>
> Meanwhile, here is a workaround that I have started to use. This
> needs only one extra line, and it does not depend on lazy
> evaluation. x is an array; the output vmask is also an array of the
> same dimensionality:
>
> vmask = (x .gt. -130. .and. x .lt. 130.)
> vmask = where (ismissing (vmask), False, vmask)
>
> This is surely not as efficient as true lazy evaluation in a single
> line, but it will do for now.

Good to know there is a workaround.
>
> --Dave
>
> David Brown wrote:
>> Hi Dave,
>>
>> This is a very interesting observation. In the NCL reference
>> manual lazy
>> expression evaluation is only documented in the context of 'if'
>> statements, which require a scalar logical expression. The
>> documentation
>> for '.and.' and '.or.' says only that the operands must be
>> logical, but
>> does not mention lazy evaluation. Apparently lazy evaluation was
>> implemented specifically for 'if' statement evaluation but was never
>> generalized to work for array logical expressions.
>>
>> This leads to the inconsistency that you have pointed out here.
>> The code
>> could easily be updated to use lazy evaluation for .and. and .or.
>> in the
>> context of array logical expressions. I am not totally confident that
>> there might not be some backwards-compatibility issue, but it does
>> seem
>> like a bug of sorts, so my inclination is to go ahead and make the
>> change, noting that the behavior should be clearly documented. The
>> development team will discuss.
>> -dave
>>
>>
>> On Jun 12, 2009, at 7:22 PM, Dave Allured wrote:
>>
>>> NCL team,
>>>
>>> Is lazy expression evaluation supposed to work for array
>>> expressions?
>>>
>>> See attached script. mask1 is scalar and shows the expected result.
>>> This is basically the example in the NCL manual under "If
>>> statements",
>>> with assignment rather than if statement.
>>>
>>> http://www.ncl.ucar.edu/Document/Manuals/Ref_Manual/
>>> NclStatements.shtml
>>>
>>> For mask2 I expect True, False, True, but NCL returns True, Missing,
>>> True. This creates problems for subsequent usage of the mask.
>>>
>>> I checked this with NCL versions 5.0.1 (pre-release, ca. May
>>> 2008) and
>>> 5.1.1 (pre-release). The problem was the same in both.
>>>
>>> uname -a
>>> Darwin mac56.psd.esrl.noaa.gov 9.7.0 Darwin Kernel Version 9.7.0:
>>> Tue
>>> Mar 31 22:54:29 PDT 2009; root:xnu-1228.12.14~1/RELEASE_PPC Power
>>> Macintosh powerpc PowerMac7,3 Darwin
>>>
>>> Please advise. Thank you for taking a look.
>>>
>>> Dave Allured
>>> CU/CIRES Climate Diagnostics Center (CDC)
>>> http://cires.colorado.edu/science/centers/cdc/
>>> NOAA/ESRL/PSD, Climate Analysis Branch (CAB)
>>> http://www.cdc.noaa.gov/psd1/
>>> ; Test program for lazy expression evaluation.
>>> ; 2009-jun-13 By Dave Allured, NOAA/PSD/CU/CIRES/CDC.
>>>
>>> begin
>>> a = (/ 1,2,3 /)
>>> a@_FillValue = 2
>>>
>>> i=1
>>> mask1 = (.not.ismissing(a(i)) .and. (a(i) .gt. 0))
>>> print (mask1)
>>>
>>> mask2 = (.not.ismissing(a) .and. (a .gt. 0))
>>> print (mask2)
>>> end
>>> _______________________________________________
>>> ncl-talk mailing list
>>> List instructions, subscriber options, unsubscribe:
>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Tue Jun 16 2009 - 18:57:58 MDT

This archive was generated by hypermail 2.2.0 : Fri Jun 19 2009 - 13:23:25 MDT