Hi Billy,
I have just a few minutes, but maybe can shed a little light on this.
I think this is the deal:
The underlying math is simply that
INTEGRAL[x to b] = INTEGRAL[a to b] - INTEGRAL[a to x]
The thing is, that this statement is true where x is a continuous
variable. Here we are on a discretized axis, where each value on our
axis actually represents a cell. As you move from low to high along
the axis the running sum is really the integral across that full cell.
But the coordinate point lies in the center of the cell (ignoring the
complexities of irregular cell sizes for this discussion). I've
attempted to draw an axis to illustrate. The carets ("^") show where
the values is located on the axis in each case.
direction of integration ==>
| 1 | 2 | 3 | 4 | 5 | 6
|
@RSUM ^ ^ ^ ^ ^ ^
"correct" ^ ^ ^ ^ ^
^
So there is a 1/2 cell shift -- @RSUM (or equivalently @IIN) would be
"correctly" placed1/2 of a cell to the right of its coordinate value.
There is also a 1/2 grid cell shift in the definite integral for the
full span -- TEST[i=1:6@SUM] is actually the integral value up to
the boundary between I=6 and I=7. I think when you apply the integral
formula above in the face of these two 1/2 cell shifts, you are seeing
a full cell shift in the result. (I haven't had time to think of a way
to really visualize if this is right ....)
Anyway there are three ways to adjust the calculation to make it just
right:
- make it "theoretically correct" by correcting all of your values
to the center point of the cell. This simply involves subtracting 1/2
of the cell value of the top cell in the integration. But the results
are not going to feel "natural" for a simple sum
LET test_rsum = test[i=@rsum]-0.5*test
- add the value of the low cell to the reverse integration
LET test_rsum_rev = test[i=@sum] - test[i=@rsum] + test[i=`lo`]
or
- shift the result with the @SHF operator
I'm afraid I don't have time to actually test any of these out.
Hopefully they are not complete nonsense!
- steve
============================
William S. Kessler wrote:
This may be a really stupid question. How does one perform
@rsum or @iin in reverse (i.e. sum from the high-index end of the
list)?
The answer I would have given before I actually looked at it in detail
would have been: subtract the running sum or integral from the definite
sum over the whole range. And indeed I have done this many times. But
the result is not quite as expected, as the following example shows:
yes? let test = {1,2,3,4,5,6}
yes? list test, test[i=@rsum], test[i=@sum] - test[i=@rsum]
X: 0.5 to 6.5
Column 1: TEST is {1,2,3,4,5,6}
Column 2: TEST[X=@RSU] is {1,2,3,4,5,6} (running sum on X)
Column 3: EX#3 is TEST[I=@SUM]-TEST[I=@RSUM]
TEST TEST EX#3
1 / 1: 1.000 1.00 20.00
2 / 2: 2.000 3.00 18.00
3 / 3: 3.000 6.00 15.00
4 / 4: 4.000 10.00 11.00
5 / 5: 5.000 15.00 6.00
6 / 6: 6.000 21.00 0.00
The second column does what we want in a running sum: the first value
is the value of the function over interval 1. The second value is the
sum of the function at intervals 1+2. The last value is the sum over
the whole range, as it should be. But I had hoped and expected that the
third column would be the reverse: The last value (i=6) should be the
last value of the function (6), the second-to-last should be the sum of
the two last values (6+5=11), and the first value should be the same
sum as if the operation were done the other way (21). But it isn't. The
whole thing is shifted by 1 interval, the value in position 6 is 0, and
the value in position 1 is not the whole sum.
Am I missing something obvious???? What is the proper way to take a
running sum or integral in reverse? It should be possible to get the
same qualities of the answer doing the operation in either direction.
Billy K
--
--
Steve Hankin, NOAA/PMEL -- Steven.C.Hankin@noaa.gov
7600 Sand Point Way NE, Seattle, WA 98115-0070
ph. (206) 526-6080, FAX (206) 526-6744
|