aboutsummaryrefslogtreecommitdiff
path: root/math/gsurfit/gs_deval.gx
blob: 38b90dac173fd9e90f0451195587ac269219b271 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

# GS_DPOL -- Procedure to evaluate the polynomial derivative basis functions.

procedure $tgs_dpol (x, npts, order, nder, k1, k2, basis)

PIXEL	x[npts]		# array of data points
int	npts		# number of points
int	order		# order of new polynomial, order = 1, constant
int	nder		# order of derivative, order = 0, no derivative
PIXEL	k1, k2		# normalizing constants
PIXEL	basis[ARB]	# basis functions

int	bptr, k, kk
PIXEL	fac

begin
	# Optimize for oth and first derivatives.
	if (nder == 0) {
	    call $tgs_bpol (x, npts, order, k1, k2, basis)
	    return
	} else if (nder == 1) {
	    call $tgs_bpol (x, npts, order, k1, k2, basis)
	    do k = 1, order {
		call amulk$t(basis[1+(k-1)*npts], PIXEL (k),
		    basis[1+(k-1)*npts], npts)
	    }
	    return
	}

	# Compute the polynomials.
	bptr = 1
	do k = 1, order {
	    if (k == 1)
		call amovk$t (PIXEL(1.0), basis, npts)
	    else if (k == 2)
		call amov$t (x, basis[bptr], npts)
	    else 
		call amul$t (basis[bptr-npts], x, basis[bptr], npts)
	    bptr = bptr + npts
	}

	# Apply the derivative factor.
	bptr = 1
	do k = 1, order {
	    if (k == 1) {
	        fac = PIXEL(1.0)
		do kk = 2, nder
		    fac = fac * PIXEL (kk)
	    } else {
	        fac = PIXEL(1.0)
		do kk = k +  nder - 1, k, -1 
	            fac = fac * PIXEL(kk)
	    }
	    call amulk$t (basis[bptr], fac, basis[bptr], npts)
	    bptr = bptr + npts
	}
end


# GS_DCHEB -- Procedure to evaluate the chebyshev polynomial derivative
# basis functions using the usual recursion relation.

procedure $tgs_dcheb (x, npts, order, nder, k1, k2, basis)

PIXEL	x[npts]		# array of data points
int	npts		# number of points
int	order		# order of polynomial, order = 1, constant
int	nder		# order of derivative, order = 0, no derivative
PIXEL	k1, k2		# normalizing constants
PIXEL	basis[ARB]	# basis functions

int	i, k
pointer	fn, dfn, xnorm, bptr, fptr
PIXEL	fac

begin
	# Optimze the no derivatives case.
	if (nder == 0) {
	    call $tgs_bcheb (x, npts, order, k1, k2, basis)
	    return
	}

	# Allocate working space for the basis functions and derivatives.
	call calloc (fn, npts * (order + nder), TY_PIXEL)
	call calloc (dfn, npts * (order + nder), TY_PIXEL)

	# Compute the normalized x values.
	call malloc (xnorm, npts, TY_PIXEL)
        call alta$t (x, Mem$t[xnorm], npts, k1, k2)

	# Compute the current solution.
        bptr = fn
        do k = 1, order + nder {
	    if (k == 1)
	        call amovk$t (PIXEL(1.0), Mem$t[bptr], npts)
	    else if (k == 2)
	        call amov$t (Mem$t[xnorm], Mem$t[bptr], npts)
	    else {
	        call amul$t (Mem$t[xnorm], Mem$t[bptr-npts], Mem$t[bptr], npts)
	        call amulk$t (Mem$t[bptr], PIXEL(2.0), Mem$t[bptr], npts)
		call asub$t (Mem$t[bptr], Mem$t[bptr-2*npts], Mem$t[bptr], npts)
	    }
	    bptr = bptr + npts
        }

	# Compute the derivative basis functions.
	do i = 1, nder {

	    # Compute the derivatives.
	    bptr = fn
	    fptr = dfn
	    do k = 1, order + nder {
		if (k == 1)
		    call amovk$t (PIXEL(0.0), Mem$t[fptr], npts)
		else if (k == 2) {
		    if (i == 1)
		        call amovk$t (PIXEL(1.0), Mem$t[fptr], npts)
		    else
		        call amovk$t (PIXEL(0.0), Mem$t[fptr], npts)
		} else {
	            call amul$t (Mem$t[xnorm], Mem$t[fptr-npts], Mem$t[fptr],
		        npts)
	            call amulk$t (Mem$t[fptr], PIXEL(2.0), Mem$t[fptr], npts)
		    call asub$t (Mem$t[fptr], Mem$t[fptr-2*npts], Mem$t[fptr],
		        npts)
		    fac = PIXEL (2.0) * PIXEL (i)
		    call awsu$t (Mem$t[bptr-npts], Mem$t[fptr], Mem$t[fptr],
			npts, fac, PIXEL(1.0))
		    
		}
	        bptr = bptr + npts
	        fptr = fptr + npts
	    }

	    # Make the derivatives the old solution
	    if (i < nder)
		call amov$t (Mem$t[dfn], Mem$t[fn], npts * (order + nder))
	}

	# Copy the solution into the basis functions.
	call amov$t (Mem$t[dfn+nder*npts], basis[1], order * npts)

	call mfree (xnorm, TY_PIXEL)
	call mfree (fn, TY_PIXEL)
	call mfree (dfn, TY_PIXEL)
end


# GS_DLEG -- Procedure to evaluate the Legendre polynomial derivative basis
# functions using the usual recursion relation.

procedure $tgs_dleg (x, npts, order, nder, k1, k2, basis)

PIXEL	x[npts]		# number of data points
int	npts		# number of points
int	order		# order of new polynomial, 1 is a constant
int	nder		# order of derivate, 0 is no derivative
PIXEL	k1, k2		# normalizing constants
PIXEL	basis[ARB]	# array of basis functions

int	i, k
pointer	fn, dfn, xnorm, bptr, fptr
PIXEL	ri, ri1, ri2, fac

begin
	# Optimze the no derivatives case.
	if (nder == 0) {
	    call $tgs_bleg (x, npts, order, k1, k2, basis)
	    return
	}

	# Allocate working space for the basis functions and derivatives.
	call calloc (fn, npts * (order + nder), TY_PIXEL)
	call calloc (dfn, npts * (order + nder), TY_PIXEL)

	# Compute the normalized x values.
	call malloc (xnorm, npts, TY_PIXEL)
        call alta$t (x, Mem$t[xnorm], npts, k1, k2)

	# Compute the basis functions.
	bptr = fn
	do k = 1, order + nder {
	    if (k == 1)
		call amovk$t (PIXEL(1.0), Mem$t[bptr], npts)
	    else if (k == 2)
		call amov$t (Mem$t[xnorm], Mem$t[bptr], npts)
	    else {
		ri = k
		ri1 = (PIXEL(2.0) * ri - PIXEL(3.0)) / (ri - PIXEL(1.0))
		ri2 = - (ri - PIXEL(2.0)) / (ri - PIXEL(1.0))
		call amul$t (Mem$t[xnorm], Mem$t[bptr-npts], Mem$t[bptr], npts)
		call awsu$t (Mem$t[bptr], Mem$t[bptr-2*npts], Mem$t[bptr],
		    npts, ri1, ri2)
	    }
	    bptr = bptr + npts
	}

	# Compute the derivative basis functions.
	do i = 1, nder {

	    # Compute the derivatives.
	    bptr = fn
	    fptr = dfn
	    do k = 1, order + nder {
		if (k == 1)
		    call amovk$t (PIXEL(0.0), Mem$t[fptr], npts)
		else if (k == 2) {
		    if (i == 1)
		        call amovk$t (PIXEL(1.0), Mem$t[fptr], npts)
		    else
		        call amovk$t (PIXEL(0.0), Mem$t[fptr], npts)
		} else {
		    ri = k
		    ri1 = (PIXEL(2.0) * ri - PIXEL(3.0)) / (ri - PIXEL(1.0))
		    ri2 = - (ri - PIXEL(2.0)) / (ri - PIXEL(1.0))
		    call amul$t (Mem$t[xnorm], Mem$t[fptr-npts], Mem$t[fptr],
		        npts)
		    call awsu$t (Mem$t[fptr], Mem$t[fptr-2*npts], Mem$t[fptr],
		        npts, ri1, ri2)
		    fac = ri1 * PIXEL (i)
		    call awsu$t (Mem$t[bptr-npts], Mem$t[fptr], Mem$t[fptr],
			npts, fac, PIXEL(1.0))
		    
		}
	        bptr = bptr + npts
	        fptr = fptr + npts
	    }

	    # Make the derivatives the old solution
	    if (i < nder)
		call amov$t (Mem$t[dfn], Mem$t[fn], npts * (order + nder))
	}

	# Copy the solution into the basis functions.
	call amov$t (Mem$t[dfn+nder*npts], basis[1], order * npts)

	call mfree (xnorm, TY_PIXEL)
	call mfree (fn, TY_PIXEL)
	call mfree (dfn, TY_PIXEL)
end