aboutsummaryrefslogtreecommitdiff
path: root/math/curfit/cvacptsd.x
blob: aa4665d8943cdafceaaac7d5075a9d4f543638b6 (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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include <math/curfit.h>

include "dcurfitdef.h"

# CVACPTS -- Procedure to add a set of points to the normal equations.
# The inner products of the basis functions are calculated and
# accumulated into the CV_ORDER(cv) by CV_NCOEFF(cv) matrix MATRIX.
# The main diagonal of the matrix is stored in the first row of
# MATRIX followed by the remaining non-zero diagonals. This method
# of storage is particularly efficient for the large symmetric
# banded matrices produced during spline fits. The inner product
# of the basis functions and the data ordinates are stored in the
# CV_NCOEFF(cv)-vector VECTOR. The array LEFT stores the
# indices which show which elements of MATRIX and VECTOR are
# to receive the inner products.

procedure dcvacpts (cv, x, y, w, npts, wtflag)

pointer	cv		# curve descriptor
double	x[npts]		# array of abcissa
double	y[npts]		# array of ordinates
double	w[npts]		# array of weights
int	npts		# number of data points
int	wtflag		# type of weighting

int	i, ii, j, k
pointer	sp
pointer	vzptr, vindex, mzptr, mindex, bptr, bbptr
pointer	bw, rows

begin

	# increment the number of points
	CV_NPTS(cv) = CV_NPTS(cv) + npts

	# remove basis functions calculated by any previous cvrefit call
	if (CV_BASIS(cv) != NULL) {

	    call mfree (CV_BASIS(cv), TY_DOUBLE)
	    call mfree (CV_WY(cv), TY_DOUBLE)

	    CV_BASIS(cv) = NULL
	    CV_WY(cv) = NULL

	    if (CV_LEFT(cv) != NULL) {
	        call mfree (CV_LEFT(cv), TY_INT)
	        CV_LEFT(cv) = NULL
	    }
	}

	# calculate weights
	switch (wtflag) {
	case WTS_UNIFORM:
	    call amovkd (double(1.0), w, npts)
	case WTS_SPACING:
	    if (npts == 1)
		w[1] = 1.
	    else
	        w[1] = abs (x[2] - x[1])
	    do i = 2, npts - 1
		w[i] = abs (x[i+1] - x[i-1])
	    if (npts == 1)
		w[npts] = 1.
	    else
	        w[npts] = abs (x[npts] - x[npts-1])
	case WTS_USER:
	    # user supplied weights
	case WTS_CHISQ:
	    # data assumed to be scaled to photons with Poisson statistics
	    do i = 1, npts {
		if (y[i] > double(0.0))
		    w[i] = double(1.0) / y[i]
		else if (y[i] < double(0.0))
		    w[i] = -double(1.0) / y[i]
		else
		    w[i] = double(0.0)
	    }
	default:
	    call amovkd (double(1.0), w, npts)
	}


	# allocate space for the basis functions
	call smark (sp)
	call salloc (CV_BASIS(cv), npts * CV_ORDER(cv), TY_DOUBLE)

	# calculate the non-zero basis functions
	switch (CV_TYPE(cv)) {
	case LEGENDRE:
	    call dcv_bleg (x, npts, CV_ORDER(cv), CV_MAXMIN(cv),
	    		  CV_RANGE(cv), BASIS(CV_BASIS(cv)))
	case CHEBYSHEV:
	    call dcv_bcheb (x, npts, CV_ORDER(cv), CV_MAXMIN(cv),
	    		  CV_RANGE(cv), BASIS(CV_BASIS(cv)))
	case SPLINE3:
	    call salloc (CV_LEFT(cv), npts, TY_INT)
	    call dcv_bspline3 (x, npts, CV_NPIECES(cv), -CV_XMIN(cv),
			      CV_SPACING(cv), BASIS(CV_BASIS(cv)),
			      LEFT(CV_LEFT(cv)))
	case SPLINE1:
	    call salloc (CV_LEFT(cv), npts, TY_INT)
	    call dcv_bspline1 (x, npts, CV_NPIECES(cv), -CV_XMIN(cv),
			      CV_SPACING(cv), BASIS(CV_BASIS(cv)),
			      LEFT(CV_LEFT(cv)))
	case USERFNC:
	    call dcv_buser (cv, x, npts)
	}


	# allocate temporary storage space for matrix accumulation
	call salloc (bw, npts, TY_DOUBLE)
	call salloc (rows, npts, TY_INT)

	# one index the pointers
	vzptr = CV_VECTOR(cv) - 1
	mzptr = CV_MATRIX(cv)
	bptr = CV_BASIS(cv)

	switch (CV_TYPE(cv)) {

	case LEGENDRE, CHEBYSHEV, USERFNC:

	    # accumulate the new right side of the matrix equation
	    do k = 1, CV_ORDER(cv) {
	        call amuld (w, BASIS(bptr), Memd[bw], npts) 
		vindex = vzptr + k
	        do i = 1, npts
	            VECTOR(vindex) = VECTOR(vindex) + Memd[bw+i-1] * y[i]
	        bbptr = bptr
	        ii = 0
	        do j = k, CV_ORDER(cv) {
		    mindex = mzptr + ii
		    do i = 1, npts
		        MATRIX(mindex) = MATRIX(mindex) + Memd[bw+i-1] *
				            BASIS(bbptr+i-1)	
		    ii = ii + 1
		    bbptr = bbptr + npts
		}
		bptr = bptr + npts
		mzptr = mzptr + CV_ORDER(cv)
	    }

	case SPLINE1,SPLINE3:

	    call amulki (LEFT(CV_LEFT(cv)), CV_ORDER(cv), Memi[rows], npts)
	    call aaddki (Memi[rows], CV_MATRIX(cv), Memi[rows], npts)
	    call aaddki (LEFT(CV_LEFT(cv)), vzptr, LEFT(CV_LEFT(cv)), npts) 

	    # accumulate the new right side of the matrix equation
	    do k = 1, CV_ORDER(cv) {
	        call amuld (w, BASIS(bptr), Memd[bw], npts) 
	        do i = 1, npts {
		    vindex = LEFT(CV_LEFT(cv)+i-1) + k
	            VECTOR(vindex) = VECTOR(vindex)+ Memd[bw+i-1] * y[i]
		}
	        bbptr = bptr
	        ii = 0
	        do j = k, CV_ORDER(cv) {
		    do i = 1, npts {
			mindex = Memi[rows+i-1] + ii
		        MATRIX(mindex) = MATRIX(mindex) + Memd[bw+i-1] *
			    BASIS(bbptr+i-1)
		    }
		    ii = ii + 1
		    bbptr = bbptr + npts
		}
	        bptr = bptr + npts
	        call aaddki (Memi[rows], CV_ORDER(cv), Memi[rows], npts)
	    }
	}

	# release the space
	call sfree (sp)
	CV_BASIS(cv) = NULL
	CV_LEFT(cv) = NULL
end