aboutsummaryrefslogtreecommitdiff
path: root/math/interp/asigrl.x
blob: d528d0be9b25d6a939b7bcf4212106e3abbed2a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

.help
		procedure asigrl

     This procedure finds the integral of the interpolant from a to b
assuming both a and b land in the array.
.endhelp

real procedure asigrl(a,b,coeff)    # returns value of integral
include "interpdef.h"
include "asidef.h"

real a,b		# integral limits
real coeff[ARB]

int na,nb,i,j,n0,nt
real s,t,ac,xa,xb,pc[6]

begin
	xa = a
	xb = b
	if ( a > b ) {		# flip order and sign at end
	    xa = b
	    xb = a
	}

	na = xa
	nb = xb
	ac = 0.		# zero accumulator

	# set number of terms
	switch (ITYPEI) {		# switch on interpolator type
	case IT_NEAREST :
	    nt = 0
	case IT_LINEAR :
	    nt = 1
	case IT_POLY3 :
	    nt = 4
	case IT_POLY5 :
	    nt = 6
	case IT_SPLINE3 :
	    nt = 4
	}

	# NEAREST_NEIGHBOR and LINEAR are handled differently because of
	# storage.  Also probably good for speed.

	if (nt == 0) {		# NEAREST_NEIGHBOR
	    # reset segment to center values
	    na = xa + 0.5
	    nb = xb + 0.5

	    # set up for first segment
	    s = xa - na

	    # for clarity one segment case is handled separately
	    if ( nb == na ) {	# only one segment involved
		t = xb - nb
		n0 = COFF + na
		ac = ac + (t - s) * coeff[n0]
	    } else {	# more than one segment

		# first segment
		n0 = COFF + na
		ac = ac + (0.5 - s) * coeff[n0]

		# middle segments
		do j = na+1, nb-1 {
		    n0 = COFF + j
		    ac = ac + coeff[n0]
		}

		# last segment
		n0 = COFF + nb
		t = xb - nb
		ac = ac + (t + 0.5) * coeff[n0]
	    }

	} else if (nt == 1) {	# LINEAR
	    # set up for first segment
	    s = xa - na

	    # for clarity one segment case is handled separately
	    if ( nb == na ) {	# only one segment involved
		t = xb - nb
		n0 = COFF + na
		ac = ac + (t - s) * coeff[n0] +
		     0.5 * (coeff[n0+1] - coeff[n0]) * (t*t - s*s)
	    } else {	# more than one segment

		# first segment
		n0 = COFF + na
		ac = ac + (1. - s) * coeff[n0] +
		     0.5 * (coeff[n0+1] - coeff[n0]) * (1. - s*s)

		# middle segments
		do j = na+1, nb-1 {
		    n0 = COFF + j
		    ac = ac + 0.5 * (coeff[n0+1] + coeff[n0])
		}

		# last segment
		n0 = COFF + nb
		t = xb - nb
		ac = ac + coeff[n0] * t + 0.5 *
			(coeff[n0+1] - coeff[n0]) * t * t
	    }

	} else {		# A higher order interpolant

	    # set up for first segment
	    s = xa - na

	    # for clarity one segment case is handled separately
	    if ( nb == na ) {	# only one segment involved
		t = xb - nb
		n0 = COFF + na
		call iigetpc(n0,pc,coeff)
		do i = 1,nt
		    ac = ac + (1./i) * pc[i] * (t ** i - s ** i)
	    } else {	# more than one segment

		# first segment
		n0 = COFF + na
		call iigetpc(n0,pc,coeff)
		do i = 1,nt
		    ac = ac + (1./i) * pc[i] * (1. - s ** i)

		# middle segments
		do j = na+1, nb-1 {
		    n0 = COFF + j
		    call iigetpc(n0,pc,coeff)
		    do i = 1,nt
			ac = ac + (1./i) * pc[i]
		}

		# last segment
		n0 = COFF + nb
		t = xb - nb
		call iigetpc(n0,pc,coeff)
		do i = 1,nt
		    ac = ac + (1./i) * pc[i] * t ** i
	    }
	}

	if ( a < b )
	    return(ac)
	else
	    return(-ac)
end

	
procedure iigetpc(n0, pc, coeff)	# generates polynomial coefficients
					# if spline or poly3 or poly5

int n0			# coefficients wanted for n0 < x n0 + 1
real coeff[ARB]

real pc[ARB]

int i,k,nt
real d[6]

begin
	# generate polynomial coefficients, first for spline.
	if (ITYPEI == IT_SPLINE3) {
	    pc[1] = coeff[n0-1] + 4. * coeff[n0] + coeff[n0+1]
	    pc[2] = 3. * (coeff[n0+1] - coeff[n0-1])
	    pc[3] = 3. * (coeff[n0-1] - 2. * coeff[n0] + coeff[n0+1])
	    pc[4] = -coeff[n0-1] + 3. * coeff[n0] - 3. * coeff[n0+1] +
				    coeff[n0+2]
		
	} else {
	    if (ITYPEI == IT_POLY5)
		nt = 6
	    else	# must be POLY3
		nt = 4

	    # Newton's form written in line to get polynomial from data

	    # load data
	    do i = 1,nt
		d[i] = coeff[n0 - nt/2 + i]

	    # generate difference table
	    do k = 1, nt-1
		do i = 1,nt-k
		    d[i] = (d[i+1] - d[i]) / k

	    # shift to generate polynomial coefficients of (x - n0)
	    do k = nt,2,-1
		do i = 2,k
		    d[i] = d[i] + d[i-1] * (k - i - 2)

	    do i = 1,nt
		pc[i] = d[nt + 1 - i]
	}

	return
end